简体   繁体   English

通过代理服务器从C#/ .NET调用Twilio API

[英]Calling Twilio API from C#/.NET through a Proxy Server

I am trying to run the C# sample twilio application from behind a proxy web server. 我试图从代理Web服务器后面运行C# sample twilio应用程序 My proxy server needs authentication. 我的代理服务器需要验证。 Using the code below, I am able to authenticate successfully to the proxy server and make the Twilio call. 使用下面的代码,我能够成功验证代理服务器并进行Twilio调用。 However, Twilio returns me a code 20003 (Permission Denied). 但是,Twilio返回给我一个代码20003 (Permission Denied)。 My AccountSID and AuthToken are correct. 我的AccountSID和AuthToken是正确的。 The below code (without the proxy settings) works just fine in a different environment that does not require a web proxy server. 以下代码(没有代理设置)在不需要Web代理服务器的不同环境中工作正常。

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. 我的问题类似于使用Java 发布 的问题和解决方案 ,但我无法使用C#/ .NET复制Java修复。 I am using .NET SDK 4.5 我使用的是.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. 我能够使用直接API调用绕过这个问题。 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();
        }
    }
}

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

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