2014-07-22 3 views
2
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]) 부분을 어떻게 피드합니까?CURL 요청 만들기 ASP.NET

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

"연결해야하는"것은 무엇을 의미합니까? 어떤 기능을 수행하려고합니까? – jbrahy

+0

죄송합니다. 기본적으로이 URL에 게시물/컬 요청을 작성해야합니다. 문자 메시지 서비스입니다. – matwonk

답변

5

여기에 Twilio 전도사가 있습니다.

우리가 같은 페이지에 있는지 확인하기 위해 Twilio API의 Message endpoint에 POST 요청을해야하지만 헬퍼 라이브러리를 사용할 수는 없습니다.

문제는 없지만 .NET의 네이티브 HTTP 클라이언트 라이브러리 인 HttpWebRequest와 HttpWebResponse를 사용할 수 있습니다. 그게 전부 같이 보일 것이다 : 당신이 그들을 필요한 경우 GetRequestStream와하는 GetResponse 방법의 비동기 버전도 있습니다

//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(); 

.

희망이 있습니다.

+0

네, 그게 제가 언급 한 것입니다. 그러나 코드를 실행할 때 400 개의 잘못된 요청 오류가 발생합니다. 무엇이 잘못되었는지 확신하지 못합니다. – matwonk

+0

나는 그것을 작동 시켰고, 나는 webrequest.ContentType = "application/x-www-form-urlencoded"을 추가해야했다. after webrequest.Method = "POST"; 코드를 편집하면 답변으로 표시됩니다. – matwonk

+0

@ devin-rader는이 답변이 유효합니까? 내가 400 나쁜 요청을 받기 때문에 – FaizanRabbani

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

실제로 Twilio C# api에 연결할 수 없습니다. 이 특정 프로젝트에서 나는 더 이상 Dll을 추가 할 수 없으므로 직접 cURL API에 연결해야합니다. – matwonk