[英]Calling Twilio API from C#/.NET through a Proxy Server
我試圖從代理Web服務器后面運行C# sample twilio應用程序 。 我的代理服務器需要驗證。 使用下面的代碼,我能夠成功驗證代理服務器並進行Twilio調用。 但是,Twilio返回給我一個代碼20003 (Permission Denied)。 我的AccountSID和AuthToken是正確的。 以下代碼(沒有代理設置)在不需要Web代理服務器的不同環境中工作正常。
我的問題類似於使用Java 發布 的問題和解決方案 ,但我無法使用C#/ .NET復制Java修復。 我使用的是.NET SDK 4.5
using System;
using Twilio;
using System.Net;
namespace TestTwilio
{
class Program
{
static void Main(string[] args)
{
var accountSid = "xx";
var authToken = "yy";
var twilio = new TwilioRestClient(accountSid, authToken);twilio.Proxy = new System.Net.WebProxy("proxy.mycompany.com", 8080);
twilio.Proxy.Credentials = new NetworkCredential(“username”, “password”);
var message = twilio.SendMessage("+1xxxxxxxxxx","+1xxxxxxxxxx", "Hello from C#");
if (message.RestException != null)
{
var error = message.RestException.Message;
Console.WriteLine(error);
Console.WriteLine(message.RestException.MoreInfo);
Console.WriteLine(message.Uri);
Console.WriteLine(message.AccountSid);
Console.Write("Press any key to continue.");
Console.ReadKey();
}
}
}
}
謝謝你的幫助。
我能夠使用直接API調用繞過這個問題。 對原始問題不是一個優雅的解決方案......所以仍然在尋找正確的方法來做到這一點。 以下是有效的代碼。
using System;
using System.Net;
using RestSharp;
namespace TestTwilio
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/{yourTwilioAccountSID}/SMS/Messages.json");
client.Proxy = new System.Net.WebProxy("proxy.mycompany.com", 8080);
client.Proxy.Credentials = new NetworkCredential("<<proxyServerUserName>>", "<<proxyServerPassword>>", "<<proxyServerDomain>>");
var request = new RestRequest(Method.POST);
request.Credentials = new NetworkCredential("<<your Twilio AccountSID>>", "<<your Twilio AuthToken>>");
request.AddParameter("From", "+1xxxxxxxxxx");
request.AddParameter("To", "+1xxxxxxxxxx");
request.AddParameter("Body", "Testing from C# after authenticating with a Proxy");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.ReadKey();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.