创建CURL请求ASP.NET

问题描述 投票:2回答:3
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]

我有上面的卷曲代码,我需要连接到的API。问题是我需要使用ASP.NET(C#)进行连接。我对ASP.NET不是很熟悉,也不知道从哪里开始。我知道如何在PHP中编写代码,但ASP.NET是另一回事。从我所做的研究中我需要使用WebRequest。如何输入请求的发布数据和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";
c# php asp.net curl webrequest
3个回答
5
投票

Twilio福音传教士在这里。

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

没问题,您可以使用.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方法的异步版本。

希望有所帮助。


0
投票

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);    
  }
}

0
投票

为我工作的代码

string accountsid =“AccountSid”; string 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密钥部分获取客户端密钥和客户端密钥 - https://www.twilio.com/docs/iam/keys/api string 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();
© www.soinside.com 2019 - 2024. All rights reserved.