Click here to Skip to main content
15,891,905 members
Articles / DevOps
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Jun 2016CPOL 30.5K   8   10
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)


Written By
Software Developer (Senior)
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question403 error Pin
sha196230-Oct-21 10:26
sha196230-Oct-21 10:26 
QuestionGet error 403 Pin
SATEGO12-Apr-21 13:10
SATEGO12-Apr-21 13:10 
AnswerRe: Get error 403 Pin
sha196230-Oct-21 10:27
sha196230-Oct-21 10:27 
GeneralRe: Get error 403 Pin
SATEGO12-Nov-21 6:40
SATEGO12-Nov-21 6:40 
QuestionHow to use for dummies Pin
myfirsttony14-Aug-19 8:14
myfirsttony14-Aug-19 8:14 
Questioni need some documents about requests structure Pin
mehdi rahmati26-Jan-19 21:41
mehdi rahmati26-Jan-19 21:41 
QuestionGetting the SMS Messages from Inbox? Pin
demotis29-Jun-16 5:09
demotis29-Jun-16 5:09 
AnswerRe: Getting the SMS Messages from Inbox? Pin
Daniel Padevet4-Jul-16 7:28
professionalDaniel Padevet4-Jul-16 7:28 
AnswerRe: Getting the SMS Messages from Inbox? Pin
DeBuffel1-Sep-19 20:39
DeBuffel1-Sep-19 20:39 
AnswerRe: Getting the SMS Messages from Inbox? Pin
DeBuffel2-Sep-19 19:16
DeBuffel2-Sep-19 19:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.