Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Equivalent to this cURL command

Tags:

c#

curl

asp.net

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, and Body.

like image 929
Grahame A Avatar asked Sep 21 '11 22:09

Grahame A


2 Answers

Try including

 request.Method = "POST";

and

request.Credentials = new NetworkCredential("username", "password");
like image 141
Bala R Avatar answered Sep 28 '22 07:09

Bala R


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.

like image 29
Frazell Thomas Avatar answered Sep 28 '22 07:09

Frazell Thomas