I am trying to run the C# sample twilio application from behind a proxy web server. My proxy server needs authentication. Using the code below, I am able to authenticate successfully to the proxy server and make the Twilio call. However, Twilio returns me a code 20003 (Permission Denied). My AccountSID and AuthToken are correct. The below code (without the proxy settings) works just fine in a different environment that does not require a web proxy server.
My issue is similar to the issue and solution posted here , using Java, but I am unable to replicate the Java fix using C#/.NET. I am using .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();
}
}
}
}
Thanks for helping out.
I was able to bypass the issue using a direct API call. Is not an elegant solution to the original problem... so still looking for the right way to do this. Below is the code that worked.
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.