Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

How to Send an SMS with TP-LINK TL-MR6400 from .NET

5.00/5 (4 votes)
11 Jun 2016CPOL 31.6K  
Short code showing how to send an SMS from C# via TP-LINK TL-MR6400 LTE Router

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.

C#
/// <summary>
/// Gets MD5 hash
/// </summary>
/// <param name="password">Password</param>
/// <returns>MD5 hash</returns>
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();
 }
}

/// <summary>
/// Send SMS with TP-LINK TL-MR6400 1.0.12 Build 160322 Rel.33912n
/// </summary>
/// <param name="ipAddress">IP Address of the TL-MR6400 device</param>
/// <param name="userName">Administrator username</param>
/// <param name="password">Password</param>
/// <param name="phoneNumber">Phone number</param>
/// <param name="message">SMS text</param>
/// <returns>True if sms was sent</returns>
public static bool SendSms(string ipAddress,
string userName, string password, string phoneNumber, string message)
{
 // Create auth cookie
 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;

 // Login
 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();
   }
  }
 }

 // Get session URI
 System.Text.RegularExpressions.Regex regex =
 new System.Text.RegularExpressions.Regex("http://.*(?=/Index.htm)");
 var hostUri = regex.Match(responseString).Value;

 // Send SMS
 // Create request
 request = (HttpWebRequest)HttpWebRequest.Create(hostUri + "/lteWebCfg");
 request.CookieContainer = new CookieContainer();
 request.CookieContainer.Add(c);
 request.Method = "POST";
 request.Referer = hostUri + "/_lte_SmsNewMessageCfgRpm.htm";

 // Not needed
 //req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
 //req.Accept = "application/json, text/javascript, */*; q=0.01";

 // Create JSON
 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"));

 // Write to request
 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");
   }
  }
 }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)