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
  • 12668 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?

Gaurav-Behl
Level 10
January 10, 2019

Hi Gaurav,

Which class should I be extending from?

When I just add

    @Reference

    private HttpClientBuilderFactory httpFactory;

it throws runtime exceptions

currently i use:

public class AEMClient extends SlingAllMethodsServlet implements OptingServlet

Thanks,
Abhijeet


This is R6+ example. Just borrow the dependencies and relevant pom.xml entries

acs-aem-commons/HttpClientFactoryImpl.java at master · Adobe-Consulting-Services/acs-aem-commons · GitHub