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.

3
  • 1
    Did this ever get sovled? I want to do it in the same way Commented May 21, 2013 at 14:30
  • Let me find out what I did and I'll get back to you! Commented May 23, 2013 at 14:32
  • could you please look at me question as I got the same issue stackoverflow.com/questions/23394895/… Commented Apr 30, 2014 at 18:09

2 Answers 2

6

Try including

 request.Method = "POST";

and

request.Credentials = new NetworkCredential("username", "password");
Sign up to request clarification or add additional context in comments.

Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.