Click here to Skip to main content
13,551,173 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.4K views
7 bookmarked
Posted 11 Jun 2016
Licenced CPOL

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

, 11 Jun 2016
Rate this:
Please Sign up or sign in to vote.
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.

/// <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)

Share

About the Author

Daniel Padevet
Software Developer (Senior)
Czech Republic Czech Republic
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGetting the SMS Messages from Inbox? Pin
demotis29-Jun-16 5:09
memberdemotis29-Jun-16 5:09 
AnswerRe: Getting the SMS Messages from Inbox? Pin
Daniel Padevet4-Jul-16 7:28
professionalDaniel Padevet4-Jul-16 7:28 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 11 Jun 2016
Article Copyright 2016 by Daniel Padevet
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid