using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Web; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; namespace esms_client { public class SMSHttpPostClient { /// /// Method for sending single SMS. /// /// Registered user name /// Valid login password /// Sender ID /// valid Single Mobile Number /// Message Content /// Department generate key by login to services portal // Method for sending single SMS. public String sendSingleSMS(String username, String password, String senderid, String mobileNo, String message, String secureKey, String templateid, String templateid) { //Latest Generated Secure Key Stream dataStream; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //forcing .Net framework to use TLSv1.2 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msdgweb.mgov.gov.in/esms/sendsmsrequestDLT"); request.ProtocolVersion = HttpVersion.Version10; request.KeepAlive = false; request.ServicePoint.ConnectionLimit = 1; //((HttpWebRequest)request).UserAgent = ".NET Framework Example Client"; ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"; request.Method = "POST"; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); String encryptedPassword = encryptedPasswod(password); String NewsecureKey = hashGenerator(username.Trim(), senderid.Trim(), message.Trim(), secureKey.Trim()); String smsservicetype = "singlemsg"; //For single message. String query = "username=" + HttpUtility.UrlEncode(username.Trim()) + "&password=" + HttpUtility.UrlEncode(encryptedPassword) + "&smsservicetype=" + HttpUtility.UrlEncode(smsservicetype) + "&content=" + HttpUtility.UrlEncode(message.Trim()) + "&mobileno=" + HttpUtility.UrlEncode(mobileNo) + "&senderid=" + HttpUtility.UrlEncode(senderid.Trim()) + "&key=" + HttpUtility.UrlEncode(NewsecureKey.Trim()) + "&templateid=" + HttpUtility.UrlEncode(templateid.Trim()); byte[] byteArray = Encoding.ASCII.GetBytes(query); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); String Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// Method for sending bulk SMS. /// /// Registered user name /// Valid login password /// Sender ID /// valid Mobile Numbers /// Message Content /// Department generate key by login to services portal // method for sending bulk SMS public String sendBulkSMS(String username, String password, String senderid, String mobileNos, String message,String secureKey, String templateid) { Stream dataStream; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //forcing .Net framework to use TLSv1.2 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msdgweb.mgov.gov.in/esms/sendsmsrequestDLT"); request.ProtocolVersion = HttpVersion.Version10; request.KeepAlive = false; request.ServicePoint.ConnectionLimit = 1; //((HttpWebRequest)request).UserAgent = ".NET Framework Example Client"; ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"; request.Method = "POST"; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); String encryptedPassword = encryptedPasswod(password); String NewsecureKey = hashGenerator(username.Trim(), senderid.Trim(), message.Trim(), secureKey.Trim()); Console.Write(NewsecureKey); Console.Write(encryptedPassword); String smsservicetype = "bulkmsg"; // for bulk msg String query = "username=" + HttpUtility.UrlEncode(username.Trim()) + "&password=" + HttpUtility.UrlEncode(encryptedPassword) + "&smsservicetype=" + HttpUtility.UrlEncode(smsservicetype) + "&content=" + HttpUtility.UrlEncode(message.Trim()) + "&bulkmobno=" + HttpUtility.UrlEncode(mobileNos) + "&senderid=" + HttpUtility.UrlEncode(senderid.Trim()) + "&key=" + HttpUtility.UrlEncode(NewsecureKey.Trim()) + "&templateid=" + HttpUtility.UrlEncode(templateid.Trim()); Console.Write(query); byte[] byteArray = Encoding.ASCII.GetBytes(query); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); String Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// method for Sending unicode.. /// /// Registered user name /// Valid login password /// Sender ID /// valid Mobile Numbers /// Unicodemessage Message Content /// Department generate key by login to services portal //method for Sending unicode message.. public String sendUnicodeSMS(String username, String password, String senderid, String mobileNos, String Unicodemessage, String secureKey, String templateid) { Stream dataStream; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //forcing .Net framework to use TLSv1.2 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msdgweb.mgov.gov.in/esms/sendsmsrequestDLT"); request.ProtocolVersion = HttpVersion.Version10; request.KeepAlive = false; request.ServicePoint.ConnectionLimit = 1; //((HttpWebRequest)request).UserAgent = ".NET Framework Example Client"; ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"; request.Method = "POST"; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); String U_Convertedmessage = ""; foreach (char c in Unicodemessage) { int j = (int)c; String sss = "" + j + ";"; U_Convertedmessage = U_Convertedmessage + sss; } String encryptedPassword = encryptedPasswod(password); String NewsecureKey = hashGenerator(username.Trim(), senderid.Trim(), U_Convertedmessage.Trim(), secureKey.Trim()); String smsservicetype = "unicodemsg"; // for unicode msg String query = "username=" + HttpUtility.UrlEncode(username.Trim()) + "&password=" + HttpUtility.UrlEncode(encryptedPassword) + "&smsservicetype=" + HttpUtility.UrlEncode(smsservicetype) + "&content=" + HttpUtility.UrlEncode(U_Convertedmessage.Trim()) + "&bulkmobno=" + HttpUtility.UrlEncode(mobileNos) + "&senderid=" + HttpUtility.UrlEncode(senderid.Trim()) + "&key=" + HttpUtility.UrlEncode(NewsecureKey.Trim())+ "&templateid=" + HttpUtility.UrlEncode(templateid.Trim()); byte[] byteArray = Encoding.ASCII.GetBytes(query); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); String Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// Method for sending OTP MSG. /// /// Registered user name /// Valid login password /// Sender ID /// valid single Mobile Number /// Message Content /// Department generate key by login to services portal // Method for sending OTP MSG. public String sendOTPMSG(String username, String password, String senderid, String mobileNo, String message, String secureKey, String templateid) { Stream dataStream; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //forcing .Net framework to use TLSv1.2 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msdgweb.mgov.gov.in/esms/sendsmsrequestDLT"); request.ProtocolVersion = HttpVersion.Version10; request.KeepAlive = false; request.ServicePoint.ConnectionLimit = 1; //((HttpWebRequest)request).UserAgent = ".NET Framework Example Client"; ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"; request.Method = "POST"; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); String encryptedPassword = encryptedPasswod(password); String key = hashGenerator(username.Trim(), senderid.Trim(), message.Trim(), secureKey.Trim()); String smsservicetype = "otpmsg"; //For OTP message. String query = "username=" + HttpUtility.UrlEncode(username.Trim()) + "&password=" + HttpUtility.UrlEncode(encryptedPassword) + "&smsservicetype=" + HttpUtility.UrlEncode(smsservicetype) + "&content=" + HttpUtility.UrlEncode(message.Trim()) + "&mobileno=" + HttpUtility.UrlEncode(mobileNo) + "&senderid=" + HttpUtility.UrlEncode(senderid.Trim()) + "&key=" + HttpUtility.UrlEncode(key.Trim())+ "&templateid=" + HttpUtility.UrlEncode(templateid.Trim()); byte[] byteArray = Encoding.ASCII.GetBytes(query); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); String Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } // Method for sending UnicodeOTP MSG. /// /// method for Sending unicode.. /// /// Registered user name /// Valid login password /// Sender ID /// valid Mobile Numbers /// Unicodemessage Message Content /// Department generate key by login to services portal //method for Sending unicode message.. public String sendUnicodeOTPSMS(String username, String password, String senderid, String mobileNos, String UnicodeOTPmsg, String secureKey, String templateid) { Stream dataStream; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //forcing .Net framework to use TLSv1.2 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msdgweb.mgov.gov.in/esms/sendsmsrequestDLT"); request.ProtocolVersion = HttpVersion.Version10; request.KeepAlive = false; request.ServicePoint.ConnectionLimit = 1; //((HttpWebRequest)request).UserAgent = ".NET Framework Example Client"; ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"; request.Method = "POST"; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); String U_Convertedmessage = ""; foreach (char c in UnicodeOTPmsg) { int j = (int)c; String sss = "" + j + ";"; U_Convertedmessage = U_Convertedmessage + sss; } String encryptedPassword = encryptedPasswod(password); String NewsecureKey = hashGenerator(username.Trim(), senderid.Trim(), U_Convertedmessage.Trim(), secureKey.Trim()); String smsservicetype = "unicodeotpmsg"; // for unicode msg String query = "username=" + HttpUtility.UrlEncode(username.Trim()) + "&password=" + HttpUtility.UrlEncode(encryptedPassword) + "&smsservicetype=" + HttpUtility.UrlEncode(smsservicetype) + "&content=" + HttpUtility.UrlEncode(U_Convertedmessage.Trim()) + "&bulkmobno=" + HttpUtility.UrlEncode(mobileNos) + "&senderid=" + HttpUtility.UrlEncode(senderid.Trim()) + "&key=" + HttpUtility.UrlEncode(NewsecureKey.Trim())+ "&templateid=" + HttpUtility.UrlEncode(templateid.Trim()); byte[] byteArray = Encoding.ASCII.GetBytes(query); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); String Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } /// /// Method to get Encrypted the password /// /// password as String" protected String encryptedPasswod(String password) { byte[] encPwd = Encoding.UTF8.GetBytes(password); //static byte[] pwd = new byte[encPwd.Length]; HashAlgorithm sha1 = HashAlgorithm.Create("SHA1"); byte[] pp = sha1.ComputeHash(encPwd); // static string result = System.Text.Encoding.UTF8.GetString(pp); StringBuilder sb = new StringBuilder(); foreach (byte b in pp) { sb.Append(b.ToString("x2")); } return sb.ToString(); } /// /// Method to Generate hash code /// /// your last generated Secure_key protected String hashGenerator(String Username, String sender_id, String message, String secure_key) { StringBuilder sb = new StringBuilder(); sb.Append(Username).Append(sender_id).Append(message).Append(secure_key); byte[] genkey = Encoding.UTF8.GetBytes(sb.ToString()); //static byte[] pwd = new byte[encPwd.Length]; HashAlgorithm sha1 = HashAlgorithm.Create("SHA512"); byte[] sec_key = sha1.ComputeHash(genkey); StringBuilder sb1 = new StringBuilder(); for (int i = 0; i < sec_key.Length; i++) { sb1.Append(sec_key[i].ToString("x2")); } return sb1.ToString(); } } } class MyPolicy : ICertificatePolicy { public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } |