Skip to content

Instantly share code, notes, and snippets.

@serkaniyigun
Created March 17, 2013 10:18
Show Gist options
  • Save serkaniyigun/5180906 to your computer and use it in GitHub Desktop.
Save serkaniyigun/5180906 to your computer and use it in GitHub Desktop.
.NET SMS Gateway Api Post cURL
public void SendSMS(string smsText, string sendTo)
{
#region Variables
string userId = ConfigurationManager.AppSettings["SMSGatewayUserID"];
string pwd = ConfigurationManager.AppSettings["SMSGatewayPassword"];
string postURL = ConfigurationManager.AppSettings["SMSGatewayPostURL"];
StringBuilder postData = new StringBuilder();
string responseMessage = string.Empty;
HttpWebRequest request = null;
#endregion
try
{
// Prepare POST data
postData.Append("action=send");
postData.Append("&username=" + userId);
postData.Append("&passphrase=" + pwd);
postData.Append("&phone=" + sendTo);
postData.Append("&message=" + smsText);
byte[] data = new System.Text.ASCIIEncoding().GetBytes(postData.ToString());
// Prepare web request
request = (HttpWebRequest)WebRequest.Create(postURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
// Write data to stream
using (Stream newStream = request.GetRequestStream())
{
newStream.Write(data, 0, data.Length);
}
// Send the request and get a response
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Read the response
using (StreamReader srResponse = new StreamReader(response.GetResponseStream()))
{
responseMessage = srResponse.ReadToEnd();
}
// Logic to interpret response from your gateway goes here
Response.Write(String.Format("Response from gateway: {0}", responseMessage));
}
}
catch (Exception objException)
{
Response.Write(objException.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment