How to get RESTEasy to use system web proxy?

本秂侑毒 提交于 2019-12-10 15:56:40

问题


I'm using JAX-RS for a RESTful client in Java 8 with RESTEasy 3.0.10.Final as the implementation on windows 7 64-bit. RESTEasy in turn uses Apache httpclient 4.2.6.

I separately learned how to manually specify a web proxy for httpclient, so if in my code I manually configure a ResteasyClientBuilder using a specially configured ApacheHttpClient4Engine, my RESTEasy client connections correctly use the proxy.

My problem is that I want to use the java.net.useSystemProxies system property so that my Java program will use whatever proxy is configured in Windows Internet Options, so that my Java program will work seamlessly with other programs and not require that it be specially configured. This works fine for connections using Java's native HTTP connections. But RESTEasy/Apache httpclient apparently opens up a direct socket and performs its HTTP communication at a low level.

When I tell Java to use system proxies, Java indeed routs the httpclient connections to the proxy. But for some reason httpclient thinks it's talking to a SOCKS proxy---or something---and the request times out:

java.net.SocketException: Can't connect to SOCKS proxy:Connection timed out: connect
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:429)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:159)

Someone else recommended turning off SOCKS routing in the default proxy selector, and this indeed prevents the httpclient socket from being routed to the system proxy---but then the HTTP connection completely bypasses the proxy, which defeats the purpose.

I'm guessing here that httpclient just asks for a socket, and Java sees the system proxy and assumes that socket connections are for a SOCKS proxy. I would also guess that, even if Java sockets were passed to the web proxy normally, httpclient wouldn't know anything about it and consequently wouldn't seen the correct proxy format of the HTTP request. Or would httpclient somehow see that java.net.useSystemProxies is turned on, and alter its request? But how would it even know whether a system proxy were truly in effect?

So I'm completely at a standstill here. If I turn on java.net.useSystemProxies and make normal HTTP requests within Java, they use the system-configured web proxy. How can I tell RESTEasy/httpclient to use the system-configured web proxy?

来源:https://stackoverflow.com/questions/29083945/how-to-get-resteasy-to-use-system-web-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!