使用来自代理

时间:2016-10-13 06:42:20

标签: java proxy twilio

我正在尝试使用Twilio-7 api发送短信。我在代理网络上。我收到了以下错误。

引起:com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:尝试全部:8个地址,但无法通过HTTPS连接到服务器:api.twilio.com port:443    在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)

我正在使用以下代码:

//Install the Java helper library from twilio.com/docs/java/install
import java.net.URISyntaxException;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;

import com.twilio.Twilio;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

import com.twilio.sdk.*;


    public class TwilioRestTest {
     // Find your Account Sid and Auth Token at twilio.com/console
     public static final String ACCOUNT_SID = "SID";
     public static final String AUTH_TOKEN = "PWD";
     public static final String PROXY_ADDRESS = "proxy-address.com";
     public static final int PROXY_PORT = 80;
     private static final String PROXY_PASSWORD = null;
     private static final String PROXY_USER = null;
     /**
     * @param args
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws URISyntaxException {

    System.setProperty("http.proxyHost",PROXY_ADDRESS );
    System.setProperty("http.proxyPort", PROXY_PORT);
         Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

         Message message = Message
                 .creator(new PhoneNumber("+1112323223"),  // to
                          new PhoneNumber("+15005550006"),  // from
                          "Where's Wallace?")
                 .create();
         System.out.println("Test:"+message.getStatus());
     }
    }

我试过方法描述here,但后来我得到了编译错误

  

TwilioRestClient客户端=新的TwilioRestClient(ACCOUNT_SID,   AUTH_TOKEN);           client.setHttpClient(HttpClient的);没有找到TwilioRestClient类型的构造函数(字符串,字符串)找不到符号找到setHttpClient   类型TwilioRestClient。

任何领导都会被推销。

1 个答案:

答案 0 :(得分:0)

试试这个: Link

主要方法:

Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
TwilioRestClient client = new TwilioRestClient.Builder(ACCOUNT_SID, AUTH_TOKEN).httpClient(new ProxyNetworkHttpClient()).build();
Twilio.setRestClient(client);
Message message = Message.creator(new PhoneNumber("TO"),new PhoneNumber("FROM"), "MESSAGE").create();