SMS API: ASP.NET / C#

Overview

The following C# function communicates with our server, makes an HTTP request and then returns our response.

To use this function:

  1. Save it in a location that can be accessed by your script - in ASP.NET, this will be your .cs file.
  2. Prepare the request, ensuring that individual values have been urlencoded.
  3. Call the function like this string response = tm4b(url, false, request_data);.

HttpWebRequest / HttpWebResponse

public string tm4b(string sURL, bool bEscapeURL, string sPostData)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string stmp = string.Empty;
Uri httpUri = new Uri(sURL, bEscapeURL);
try
{
System.Text.ASCIIEncoding byteConverter = new System.Text.ASCIIEncoding();
byte[] byte1 = byteConverter.GetBytes(sPostData);
System.Net.HttpWebRequest hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(httpUri);
hwRequest.ContentType = "application/x-www-form-urlencoded";
hwRequest.Method = "POST";
hwRequest.ContentLength = sPostData.Length;
System.IO.Stream PostStream = hwRequest.GetRequestStream();
PostStream.Write(byte1, 0, byte1.Length);
System.Net.HttpWebResponse hwResponse = (System.Net.HttpWebResponse)hwRequest.GetResponse();
System.IO.StreamReader sRead = new System.IO.StreamReader(hwResponse.GetResponseStream(),
System.Text.Encoding.ASCII);

while((stmp=sRead.ReadLine()) != nulll)
{
sb.Append(stmp + "\r\n");
}

PostStream.Close();
sRead.Close();

return sb.ToString();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
return string.Empty;
}
asp.net
c#
sms
© 2003 - 2008 SMS-Comms UK Ltd. | Service Agreement
|SEND SMS|RECEIVE SMS|SMS ONLINE|SMS API|RESOURCES|
Contact Us Client Login Register Online