ASP.NET相当于这个cURL命令 [英] ASP.NET Equivalent to this cURL command
问题描述
我与www.twilio.com的API,在工作,它提供了PHP和Ruby的例子。我工作的一个网站通过ASP.NET MVC 3他们的API多数民众赞成在codeD发送短信,并通过我有限的WebRequest对象的知识,翻译如下:
I'm working with the api over at www.twilio.com and it provides examples in php and ruby. I'm working on a site to send text messages through their api thats coded 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
这个:
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();
但它不是验证,我得到他们的API 401。什么是等价的C#的卷曲 -u
命令?
更新
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();
仍然得到401任何想法?
Still getting 401. Any ideas?
更新2
好了,多亏了下面的答案我是打不通的API,但现在我得到一个400错误的请求。有没有建立一个查询字符串一起传递数据的更清洁的方式?这三个字段是从
,到
和机身
。
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
.
推荐答案
尝试包括
request.Method = "POST";
和
request.Credentials = new NetworkCredential("username", "password");
这篇关于ASP.NET相当于这个cURL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!