I am trying to configure proxy settings with Authentication for Jersey Rest client. Initially, I was using the following code that worked fine with proxy that does not require authentication
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
However, if a proxy required authentication it did not work.
I tried using Jersey Apache http client with HttpClient 4.2.1 from HttpComponents project with the following code snippet.
final ApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
Map<String, Object> configProp = config.getProperties();
final String proxyHost = System.getProperty("http.proxyHost");
final String proxyPort = System.getProperty("http.proxyPort");
if(proxyHost != null && proxyPort != null){
configProp.put(DefaultApacheHttpClientConfig.PROPERTY_PROXY_URI, "http://" + proxyHost + ":" + proxyPort);
final String proxyUser = System.getProperty("http.proxyUser");
final String proxyPassword = System.getProperty("http.proxyPassword");
if(proxyUser != null && proxyPassword != null){
ApacheHttpClientState state = config.getState();
state.setProxyCredentials(AuthScope.ANY_REALM, proxyHost, Integer.parseInt(proxyPort), proxyUser, proxyPassword);
}
}
It throws “ClassNotFoundException” error at config.getState(). It looks like path for Credentials class in HttpClient has changed and jersey Apache Http Client cannot find the Credentials class.
So, my question is “Is there a different way of handling proxy authentication in recent versions of Jersey or Jersey in General? Am I doing it wrong?”
Thanks
Ok There seems to be a dependency problem between Jersey Apache Client library and HttpComponents HttpClient library. I used Commons HttpClient library with Jersey Apache Client library and it worked perfectly fine.