Created
June 20, 2011 17:38
-
-
Save mheadd/1036096 to your computer and use it in GitHub Desktop.
C# console application to send an SMS message through SMSified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Net; | |
using System.IO; | |
namespace smsifiedexample | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
// SMSified API endpoint. | |
string webTarget = "https://api.smsified.com/v1/smsmessaging/outbound/{0}/requests"; | |
// Parameters to send with API request. | |
string webPost = "address={0}&message={1}"; | |
// SMSified credentials. | |
string userName = ""; | |
string password = ""; | |
string senderNumber = ""; | |
// Create new HTTP request. | |
string url = String.Format(webTarget, senderNumber); | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); | |
req.Method = "POST"; | |
req.ContentType = "application/x-www-form-urlencoded"; | |
byte[] postData = Encoding.ASCII.GetBytes(String.Format(webPost, "14075551212", "This is a test from C#")); | |
req.ContentLength = postData.Length; | |
// Set HTTP authorization header. | |
string authInfo = userName + ":" + password; | |
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); | |
req.Headers["Authorization"] = "Basic " + authInfo; | |
// Send HTTP request. | |
Stream PostStream = req.GetRequestStream(); | |
PostStream.Write(postData, 0, postData.Length); | |
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); | |
// Evaluate result of HTTP request. | |
if(res.StatusCode == HttpStatusCode.Created) { | |
Console.WriteLine("SMS message has been sent."); | |
} | |
else | |
{ | |
Console.WriteLine("ERROR: There was a problem sending your messaging."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment