简体   繁体   English

在C#中使用Twilio语音api进行代理身份验证

[英]Proxy authentication with Twilio voice api in C#

I want make outbound calls from my console application using C# With Twilio voice api. 我想使用带有Twilio语音api的C#从控制台应用程序拨打电话。 But most of the time i am getting the connection error message. 但是大多数时候我都会收到connection error消息。 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. Twilio传播者在这里。

If you're trying to use the proxy here, I think using our Rest API is going to be helpful. 如果您尝试在此处使用代理,我认为使用我们的Rest API会有所帮助。 Try this code below to hook your proxy server up to the HttpClient object: 请尝试以下代码,将代理服务器连接到HttpClient对象:

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 很高兴为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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