ASP.NET Equivalent to this cURL command
Solution 1
Try including
request.Method = "POST";
and
request.Credentials = new NetworkCredential("username", "password");
Solution 2
The -u
option in Curl is to specify a username and password for Server Authentication.
For C# this is set using the WebRequest.Credentials property.
Comments
-
Grahame A almost 2 years
I'm working with the Twilio API and it provides examples in PHP and Ruby. I'm working on a site to send text messages through the API that's coded in ASP.NET MVC 3, and using my limited knowledge of the WebRequest object, I was able to translate 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
Into this:
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's not authenticating, I'm getting a 401 from their API. What is the equivalent 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
Alright, thanks to the answers below I was able to get through to the api, but now I'm getting a 400 Bad Request. Is there a cleaner way to build a query string to pass this data along? The three fields are
From
,To
, andBody
.