简体   繁体   中英

Proxy authentication with Twilio voice api in C#

I want make outbound calls from my console application using C# With Twilio voice api. But most of the time i am getting the connection error message. My system is using the proxy server. So i want to add proxy authentication with the code. Please suggest.

My code is as below :

const string accountSid = "*****************";
const string authToken = "*****************"; 
var client = new TwilioRestClient(accountSid, authToken); TwilioClient.Init(accountSid, authToken); 
var to = new PhoneNumber("*****************"); 
var from = new PhoneNumber("*****************"); `enter code here`
var call = CallResource.Create(to, from, url: new Uri(tempURL));

Twilio Evangelist here.

If you're trying to use the proxy here, I think using our Rest API is going to be helpful. Try this code below to hook your proxy server up to the HttpClient object:

public static HttpClient GetProxy()
{
    // First create a proxy object
    var proxyUri = $"{proxyServerSettings.Address}:{proxyServerSettings.Port}";

    var proxyCreds = new NetworkCredential("proxyuser", "proxypassword");

    var proxy = new WebProxy(proxyUri, false)
    {
        UseDefaultCredentials = false,
        Credentials = proxyCreds,
    };

    // Now create a client handler which uses that proxy
    var httpClientHandler = new HttpClientHandler()
    {
        Proxy = proxy,
        PreAuthenticate = true,
        UseDefaultCredentials = false,
    };

    return new HttpClient(httpClientHandler);
}

To make the telephone call with the proxy, you can use the sample code here:

const string accountSid = "*****************";
const string authToken = "*****************";
string to = "+1xxxxxxxxxx";
string from = "+1xxxxxxxxxx";
string callUrl = $"https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Calls";
var httpClient = GetProxy();
var authorizationValue = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{accountSid}:{authToken}"));
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {authorizationValue}");
var result= httpClient.PostAsync(callUrl, new FormUrlEncodedContent(new Dictionary<string,string>
 {
     {"To", to},
     {"From", from},
     {"Url", "http://demo.twilio.com/docs/voice.xml"}
 }));

Let me know if this helps or if you run into additional problems. Happy to help here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM