HttpClient is not passing through proxy | Community
Skip to main content
Level 3
November 4, 2016

HttpClient is not passing through proxy

  • November 4, 2016
  • 1 reply
  • 12666 views

Am trying to consume a web service which is running on different sub-domain but on the same domain. I am getting java.net.ConnectException: Connection timed out. Below is code which I am using to call the webservice from OSGI Component class.


       String  url = "https://banking.pnc.com";
        PostMethod post = null;
        BufferedReader br = null;
        try {
            HttpClient httpClient = new HttpClient();
            post = new PostMethod(url);
            post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

            String json_Params = "{\"token\":\"" + token \"}";
            StringRequestEntity requestEntity = new StringRequestEntity(json_Params, "application/json", "UTF-8");
            post.setRequestEntity(requestEntity);

            int statusCode = httpClient.executeMethod(post);
            logger.debug("statusCode :::"+statusCode);
            response.setStatus(statusCode);
           

        } catch (Exception e) {
            logger.error("Error while ", e);
        } finally {

        }

Configured the proxy settings in OSGI Config "Day Commons HTTP Client 3.1", This request is not passing through the proxy , because of this am getting Connection timed out. Added the following proxy configuration to the HttpClient in the code and this is working good, it is passing thru proxy server.

//        HostConfiguration config = httpClient.getHostConfiguration();
//        config.setProxy(HTTP_Proxy_URL,Http_Proxy_port);

//      int statusCode = httpClient.executeMethod(config, post);

if we configure the proxy settings in OSGI Config "Day Commons HTTP Client 3.1", will HttpClient instance automatically picks proxy settings? or we need to pass the proxy settings in the code as shown above?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

abhinarvekar
Level 2
January 9, 2019

I'm using AEM 6.3 and have the same issue.

I'm making a request to external server using apache http-client.

AEM is behind a proxy. Proxy is set in Apache HTTP components proxy configuration. I expect my http client to use the same proxy by default.

Any idea what I am doing wrong

Gaurav-Behl
Level 10
January 9, 2019

Is that external service hosted on HTTPS? If yes, did you configure TLS settings in your code?

could you share the error from server logs and request/response including headers?

abhinarvekar
Level 2
January 9, 2019

I have access to both HTTP and HTTPS versions of the service.

Simply the connection times out as the http-client cannot access external world without a proxy

If I add this:

httpClientBuilder.setProxy(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()));

it just works fine.

But I want it to use default proxy set in AEM so I don't need to provide any field for setting proxy.

I tried : HttpClientBuilder.create().useSystemProperties() but that doesn't work.

Thanks,
Abhijeet