Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a http proxy for Jersey2 Client

It's easy to set a proxy for client on Jersey1.x:

config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUrl);

But how to add a http proxy for Jersey2.x client? I checked the source code and didn't find the implementation does that in:

org.glassfish.jersey.client.HttpUrlConnector

Thanks!

like image 661
feuyeux Avatar asked Sep 22 '13 10:09

feuyeux


People also ask

What is proxy in HTTP client?

What Does HTTP Proxy Mean? An HTTP Proxy serves two intermediary roles as an HTTP Client and an HTTP Server for security, management, and caching functionality. The HTTP Proxy routes HTTP Client requests from a Web browser to the Internet, while supporting the caching of Internet data.


2 Answers

thanks @feuyeux, the solution is work for me, ps, the code below is works in the proxy with http basic auth:

    ClientConfig config = new ClientConfig();
    config.connectorProvider(new ApacheConnectorProvider());
    config.property(ClientProperties.PROXY_URI, proxy);
    config.property(ClientProperties.PROXY_USERNAME,user);
    config.property(ClientProperties.PROXY_PASSWORD,pass);
    Client client = JerseyClientBuilder.newClient(config);

hope to help others

like image 191
NGloom Avatar answered Nov 09 '22 22:11

NGloom


To set different proxy on runtime is not good solution. Accordingly, I used apache connector to do so:

add apache connector dependency defined:

<dependency>
 <groupId>org.glassfish.jersey.connectors</groupId>
 <artifactId>jersey-apache-connector</artifactId>
</dependency>

add apache connector to client

config.property(ApacheClientProperties.PROXY_URI, proxyUrl); 
Connector connector = new ApacheConnector(config); 
config.connector(connector); 
like image 29
feuyeux Avatar answered Nov 09 '22 23:11

feuyeux