ASP.NET Equivalent to this cURL command
Asked Answered
I

2

7

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.

Inscrutable answered 21/9, 2011 at 22:43 Comment(3)
Did this ever get sovled? I want to do it in the same wayCareaga
Let me find out what I did and I'll get back to you!Inscrutable
could you please look at me question as I got the same issue #23395395Viscosity
K
6

Try including

 request.Method = "POST";

and

request.Credentials = new NetworkCredential("username", "password");
Kranz answered 21/9, 2011 at 22:47 Comment(0)
T
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.

Tannatannage answered 21/9, 2011 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.