Introduction
This code allows you to send an SMS via TP-LINK TL-MR6400 LTE Router.
Using the Code
First, we need to log in to administration interface using user name and password, get the session based URI and create JSON request to send an SMS.
private static string GetMD5Hash(string password)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
var bytes = md5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(password));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString("X2"));
}
return sb.ToString().ToLower();
}
}
public static bool SendSms(string ipAddress,
string userName, string password, string phoneNumber, string message)
{
string authString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes
(string.Format("{0}:{1}", userName, GetMD5Hash(password))));
System.Net.Cookie c = new System.Net.Cookie("Authorization",
"Basic " + authString, "/", ipAddress);
string responseString = null;
var request = (HttpWebRequest)HttpWebRequest.Create
(string.Format("http://{0}/userRpm/LoginRpm.htm?Save=Save", ipAddress));
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(c);
using (var resp = request.GetResponse())
{
using (var str = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(str))
{
responseString = sr.ReadToEnd();
}
}
}
System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex("http://.*(?=/Index.htm)");
var hostUri = regex.Match(responseString).Value;
request = (HttpWebRequest)HttpWebRequest.Create(hostUri + "/lteWebCfg");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(c);
request.Method = "POST";
request.Referer = hostUri + "/_lte_SmsNewMessageCfgRpm.htm";
string sms = string.Format("{{\"module\":\"message\",
\"action\":3,\"sendMessage\":{{\"to\":\"{0}\",\
"textContent\":\"{1}\",\"sendTime\":\"{2}\"}}}}",
phoneNumber, message, DateTime.Now.ToString("yyyy,m,dd,HH,MM,ss"));
var smsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sms);
using (var reqstr = request.GetRequestStream())
{
reqstr.Write(smsBytes, 0, smsBytes.Length);
reqstr.Flush();
}
using (var resp = request.GetResponse())
{
using (var str = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(str))
{
responseString = sr.ReadToEnd();
return responseString.Contains("\"result\":0");
}
}
}
}