I work with api at www.twilio.com and it gives examples in php and ruby. I am working on a website to send text messages through their api encoded in ASP.NET MVC 3, and through my limited knowledge of the WebRequest object, translated this:
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/AC4840da0d7************f98b20b084/SMS/Messages.xml' \ -d 'From=%2B14155992671' \ -u AC4840da0d7************f98b20b084:f7fc2**************75342
in it:
var request = WebRequest.Create(MessageApiString + "?From=+14*********1&To=" + Phone + "&Body=" + smsCampaign.Message); var user = "AC4840da0d7************f98b20b084"; var pass = "f7fc2**************75342"; string credentials = String.Format("{0}:{1}", user, pass); request.Headers.Add("Authorization", credentials); var result = request.GetResponse();
but it does not authenticate, I get 401 from their API. What is the equivalent of c # to the cURL -u command?
Update
var request = WebRequest.Create(MessageApiString + "?From=+14155992671&To=" + Phone + "&Body=" + smsCampaign.Message); var cc = new CredentialCache(); cc.Add(new Uri(MessageApiString), "NTLM", new NetworkCredential("AC4840da0d7************f98b20b084", "f7fc2**************75342")); request.Credentials = cc; request.Method = "POST"; var result = request.GetResponse();
Still getting 401. Any ideas?
Update 2
Ok, thanks to the answers below, I was able to switch to api, but now I get 400 Bad Request. Is there a cleaner way to build a query string to pass this data? Three fields: From , To and Body .
Gallen
source share