How to set Proxy by jersey client
https://stackoverflow.com/questions/10415607/jersey-client-set-proxy#
There is an easier approach, if you want to avoid more libraries in legacy projects and there is no need for Proxy Authentication:
-1. First you need a class which implements HttpURLConnectionFactory:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
public class ConnectionFactory implements HttpURLConnectionFactory {
Proxy proxy;
private void initializeProxy() {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 3128));
//proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 1047));
}
public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
initializeProxy();
return (HttpURLConnection) url.openConnection(proxy);
}
}
-2. Second is to instantiate an com.sun.jersey.client.urlconnection.URLConnectionHandler
URLConnectionClientHandler ch = new URLConnectionClientHandler(new ConnectionFactory());
-3. Third is to use the Client Constructor instead of Client.create:
Client client = new Client(ch);
Of course you can customize the initializing of the Proxy in the ConnectionFactory.