Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 2.0 Get Post Call via Proxy

I am using Jersey 2.4.1 for rest and want to make a GET or Post call via HTTP and HTTPS proxy. I am unable to do it. I have searched on internet and found many links but most of it are obsolete now. Some help will be really helpful as there are lot of changes from Jersey 1.X to 2.X

This is my code to make GET call(which is working fine). I want to modify it to make this call via HTTP and HTTPS proxy. Any pointers will be helpful.

javax.ws.rs.core.Response response = null;
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url); //url is string
response = target.request().header("Authorization", header).accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get();
like image 923
learner Avatar asked Apr 24 '15 11:04

learner


1 Answers

Try using the ClientConfiguration object, set whatever properties you want, and then set the configuration using ClientBuilder.withConfig(Configuration config). Then you can build it with the build() method. Have a look at this example:

ClientConfig cc = new ClientConfig();
cc.property(ClientProperties.PROXY_URI, "8.8.8.8:80");
Client client = JerseyClientBuilder.withConfig(cc).build();

This does however only work for http proxies. To set a https proxy you have to set the system properties like so:

System.setProperty("http.proxyHost", "some.proxy");
System.setProperty("http.proxyPort", "3476");
System.setProperty("https.proxyHost", "some.https.proxy");
System.setProperty("https.proxyPort", "6235");

Read this for further information.

like image 56
Distjubo Avatar answered Nov 01 '22 06:11

Distjubo