iamj
iamj

Reputation: 183

Java:HttpPost to API - ConnectTimeoutException - Apache HttpClient 4

Currently I'm working on a project, my module requires to call one of our external API using HttpClient.

I manually accessed the API using POSTMAN and I can successfully connect and get the result, but when I run the code I encountered the org.apache.http.conn.ConnectTimeoutException.

External API:

 http://10.9.11.222:8500/api/getDocs

 PostToApi class:

  HttpPost httpPost = new HttpPost("http://10.9.11.222:8500/api/getDocs");
    StringEntity stringEntity = new StringEntity(jsonToBeSent);     
    httpPost.addHeader("Content-type", "application/json");
    httpPost.setEntity(stringEntity);

    response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");

       serverResponse[0]=String.valueOf(response.getStatusLine().getStatusCode());
    serverResponse[1]=responseString;

Exception encountered:

org.apache.http.conn.ConnectTimeoutException: Connect to 10.9.11.222:8500 [/ 10.9.11.222] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:143)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:117)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at element.bst.elementexploration.rest.util.PostToServer.post(PostToServer.java:58)

Upvotes: 2

Views: 2139

Answers (2)

asch
asch

Reputation: 1963

I recommend to use fluent API of the HttpClient, if you client should not do any complicated stuff. This helps to avoid boilerplate code and write clear and simple code. All the code above may be substituted with this:

HttpResponse response = Request.Post("http://10.9.11.222:8500/api/getDocs")
            .bodyString(json, ContentType.APPLICATION_JSON)     
            .viaProxy(new HttpHost("xx.xx.xx.xx", proxyport))
            .execute().returnResponse();
ContentResponseHandler contentHandler = new ContentResponseHandler();
serverResponse[0] = response.getStatusLine().getStatusCode();
serverResponse[1] = contentHandler.handleEntity(response.getEntity()).asString();

You can read more about the fluent API here.

Upvotes: 0

iamj
iamj

Reputation: 183

I've found the solution of my problem: First thing that I forgot is that I'm using a proxy but I didn't configure my HttpClient to use it during the fetching of the API.

My Solution: Add the proxy in PostToApi class

    HttpHost proxy = new HttpHost("xx.xx.xx.xx",proxyport,"http");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();

Complete PostToApi class with the proxy

    HttpHost proxy = new HttpHost("xxx.xxx.xxx.xxx",port,"http");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();

    HttpPost httpPost = new HttpPost("http://10.9.11.222:8500/api/getDocs");
    StringEntity stringEntity = new StringEntity(jsonToBeSent);     
    httpPost.addHeader("Content-type", "application/json");
    httpPost.setEntity(stringEntity);

    response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");

    serverResponse[0]=String.valueOf(response.getStatusLine().getStatusCode());
    serverResponse[1]=responseString;

Upvotes: 1

Related Questions