2016-04-28 39 views
1

我正在使用ApacheConnection提供程序的球衣客戶端。如何使用Jersey和Apache Http客戶端進行代理驗證?

Builder builder = RequestConfig.custom().setConnectTimeout(timeout); 
    List<Proxy> proxies = ProxyManager.getInstance().select(baseUrl.toURI()); 
    if (useProxy) { 
     ... 
     builder.setProxy(new HttpHost(proxyUri.getHost(), proxyUri.getPort())); 
    } 
    RequestConfig requestConfig = builder.build(); 

    final ClientConfig clientConfig = new ClientConfig(); 
    clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig); 
    clientConfig.connectorProvider(new ApacheConnectorProvider()); 

    client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(getSSLContext()).build(); 
    client.property(ClientProperties.CONNECT_TIMEOUT, 5000); 

但是如何爲代理驗證添加用戶名和密碼?

看起來像apache連接提供程序不使用標準的java代理選擇器機制。

+0

你可以使用'DefaultProxyRoutePlanner':'DefaultProxyRoutePlanner routePlanner =新DefaultProxyRoutePlanner(新HttpHost());'。然後你設置客戶端的路由規劃器:'HttpClient client = HttpClients.custom()。setRoutePlanner(routePlanner).build();'。 – aribeiro

+0

但是在哪裏設置用戶名和密碼? – gorootde

+0

爲此,請看下面的答案:http://stackoverflow.com/a/36907908/1346996。 – aribeiro

回答

1

我終於自己找到了解決方案。不幸的是,這是無處記載:

  HttpHost proxyhost = new HttpHost(host,pw); 

      CredentialsProvider credsProvider = new BasicCredentialsProvider(); 

      credsProvider.setCredentials(new AuthScope(proxyhost), new UsernamePasswordCredentials(user, pw)); 
      clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credsProvider); 

      builder.setProxy(proxyhost); 
相關問題