2

I have implemented a PerformHttpPostRequest function which is supposed to send a post request contains a JSON type body and get a JSON response via Apache HttpClient.

public static String PerformHttpPostRequest(String url, String requestBody) throws IOException {

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);

StringEntity entity = new StringEntity(requestBody);

httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

CloseableHttpResponse response = client.execute(httpPost);

HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();

return (new BufferedReader(new InputStreamReader(is, "UTF-8"))).readLine();
}

The problem is, the code works perfect on developing environment, but when running the war file with a tomcat server but the request is not executed.

I've tried adding several catch blocks such as IOException, Exception and the code doesn't get there.

I've added debug prints which demonstrated that the code stops responding at the client.execute(...) command.

The function is called inside a try block, and after executing the .execute(...) command the code does get to the finally block.

I've already searched for a similar problem and didn't find an answer.

Is it a known issue? Does anyone have any idea of what can cause that? Or how can I fix it?

piet.t
  • 11,718
  • 21
  • 43
  • 52

4 Answers4

0

Hi Talor nice to meet you, Please try to use HttpURLConnection to solve this issue like so:

Java - sending HTTP parameters via POST method easily

Have a nice day.

el profesor

Gold100
  • 124
  • 13
  • 1
    This shouldn't be the accepted answer. Apache HttpClient does in fact work, so the solution isn't just "use another library". Of course, the question doesn't even describe what the problem is, so the solution could just as easily have been "have you tried restarting your computer?" – Christopher Schultz May 09 '18 at 16:53
0

I have tried with RestTemplate.

RequestObject requestObject = new RequestObject();
        requestObject.setKey("abcd");
        requestObject.setEndpoint(serviceEndPoint);

        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<RequestObject> requestBody = new HttpEntity<RequestObject>(
                requestObject);

        ResponseEntity<RequestObject> result = restTemplate.postForEntity(
                serviceEndPoint, requestBody, RequestObject.class);

Its very simple and hassle free, hope it helps

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
0

Few things you can try out. - Try to do ping/curl from that box where you are running tomcat. - Try to have a test method which make a get request to a server which is always reachable. For ex google.com and print the status. That way you could be able to know that you code is actually working or not in server env. Hope this helps. :)

A_01
  • 1,021
  • 11
  • 27
0

If the code doesn't pass beyond client.execute(...) but it does execute the finally block in the calling code, then you can find out what caused the aborted execution by adding this catch block to the try block that contains the finally:

catch(Throwable x) {
    x.printStackTrace();
}

Throwable is the superclass for all exception and error classes, so catching a Throwable will catch everything.

Cuspy Code
  • 184
  • 1
  • 5