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!
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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With