创建CURL请求

本文关键字:请求 CURL 创建 | 更新日期: 2023-09-27 18:13:54

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/AC053acaaf55d75ef32233132196e/Messages.json' '
--data-urlencode 'To=5555555555'  '
--data-urlencode 'From=+15555555555'  '
--data-urlencode 'Body=Test' '
-u AC053acaaf55d75a393498192382196e:[AuthToken]

我有上面的curl代码,我需要连接到一个API。问题是我需要使用ASP连接。NET (c#)。我对ASP不太熟悉。. NET,不知道从哪里开始。我知道如何在PHP编码,但ASP。NET是另一回事。根据我所做的研究,我需要使用WebRequest。我如何提供post数据和请求的authtoken (-u AC053acaaf55d75a393498192382196e:[authtoken])部分。

string url = "https://api.twilio.com/2010-04-01/Accounts/AC053acaaf55d75ef32233132196e/Messages.json";
WebRequest myReq = WebRequest.Create(url);
myReq.Method = "POST";

创建CURL请求

Twilio evangelist here

只是为了确保我们在同一个页面上,你需要在Twilio API中向theMessages端点发出POST请求,但是你不能使用我们的helper库。

没问题,你可以使用。net原生HTTP客户端库,HttpWebRequest和HttpWebResponse。它看起来像这样:

//Twilio Credentials
string accountsid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string authtoken = "asdsadasdasdasdasdsadsaads";
//Twilio API url, putting your AccountSid in the URL
string urltemplate = "https://api.twilio.com/2010-04-01/Accounts/{0}/Messages.json";
string url = string.Format(urltemplate, accountsid);
//Create a basic authorization
string basicauthtoken = string.Format("Basic {0}", System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(accountsid + ":" + authtoken)));
//Build and format the HTTP POST data
string formencodeddata = "To=+15555555555&From=+15556666666&Body=Hello World";
byte[] formbytes = System.Text.ASCIIEncoding.Default.GetBytes(formencodeddata);
//Create a new HTTP request object, set the method to POST and write the POST data to it
var webrequest = (HttpWebRequest)WebRequest.CreateHttp(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.Headers.Add("Authorization", basicauthtoken);
using (Stream postStream = webrequest.GetRequestStream()) {
    postStream.Write(formbytes, 0, formbytes.Length);
}
//Make the request, get a response and pull the data out of the response stream
var webresponse = (HttpWebResponse)webrequest.GetResponse();
Stream responseStream = webresponse.GetResponseStream();
var reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();

如果你需要GetRequestStream和GetResponse方法,也有异步版本。

希望对你有帮助。

Twilio有一些很棒的文档:http://www.twilio.com/docs/api/rest/making-calls这里还有一个很棒的c#库;twilio.com/docs/csharp/install但这里有一个c#的例子,展示了如何进行调用。

using System;
using Twilio;
class Example {
  static void Main(string[] args) {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "AC3094732a3c49700934481addd5ce1659";
    string AuthToken = "{{ auth_token }}";
    var twilio = new TwilioRestClient(AccountSid, AuthToken);
    var options = new CallOptions();
    options.Url = "http://demo.twilio.com/docs/voice.xml";
    options.To = "+14155551212";
    options.From = "+14158675309";
    var call = twilio.InitiateOutboundCall(options);
    Console.WriteLine(call.Sid);    
  }
}

为我工作的代码

string accountsid = " accountsid ";

authtoken = " authtoken ";
        //Twilio API url, putting your AccountSid in the URL
        string urltemplate = "https://api.twilio.com/2010-04-01/Accounts/{0}/Messages.json";
        string url = string.Format(urltemplate, accountsid);

//从API Keys部分获取客户端密钥和客户端密钥——https://www.twilio.com/docs/iam/keys/apistring basicauthtoken = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("ClientSecret:ClientKey"));

        //Build and format the HTTP POST data
        string formencodeddata = "To={To}&From={From}&Body={Body}";
        byte[] formbytes = System.Text.ASCIIEncoding.Default.GetBytes(formencodeddata);
        //Create a new HTTP request object, set the method to POST and write the POST data to it
        var webrequest = (HttpWebRequest)WebRequest.CreateHttp(url);
        webrequest.Method = "POST";
        webrequest.ContentType = "application/x-www-form-urlencoded";
        webrequest.Headers.Add("Authorization", basicauthtoken);
        using (Stream postStream = webrequest.GetRequestStream())
        {
            postStream.Write(formbytes, 0, formbytes.Length);
        }
        //Make the request, get a response and pull the data out of the response stream
        var webresponse = (HttpWebResponse)webrequest.GetResponse();
        Stream responseStream = webresponse.GetResponseStream();
        var reader = new StreamReader(responseStream);
        string result = reader.ReadToEnd();