Using web proxy with Java 8 JAX-RS RESTEasy client

2019-06-16 03:15发布

问题:

I can't seem to get JAX-RS clients to use a web proxy on Java 8. I'm using RESTEasy 3.0.10.Final, and running from inside Eclipse 4.4.2 on Windows 7 Professional 64-bit.

I set up a FreeProxy server on localhost running at 192.168.1.123:3128. I turn logs on and telnet to 192.168.1.123 3128 and issue a manual GET. The request shows up in the logs.

I then fire up my Java application, setting http.proxyHost=192.168.1.123 and http.proxyPort=3128 in the system properties. (I've even tried it using -D when starting the JVM.) (Note that I wouldn't expect the localhost problem to come into play, as I'm connecting to an actual IP address, not to localhost.)

I create a JAX-RS client using ClientBuilder.newBuilder().build() and perform a GET to a resource. Nothing shows up in the FreeProxy logs.

What do I have to do in order to get JAX-RS clients to use a proxy?

回答1:

The ResteasyClientBuilder provides a method to define the defaultProxy:

ResteasyClient client = new ResteasyClientBuilder().defaultProxy("localhost", 8080, "http").build();


回答2:

It seems to be possible to make RESTeasy use Java's proxy properties (e.g. -Dhttp.proxyHost) by using a different engine instead of HttpClient. java.net.HttpURLConnection supports proxy properties out of the box:

ResteasyClient client = new ResteasyClientBuilder().httpEngine(URLConnectionEngine()).build();