我已经实现了PerformHttpPostRequest函数,该函数应该发送包含JSON类型主体的发布请求,并通过Apache HttpClient获得JSON响应。
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();
}
问题是,代码在开发环境中运行完美,但是当使用tomcat服务器运行war文件但请求未执行时。
我尝试添加几个catch块,例如IOException , Exception和代码未到达那里。
我添加了调试打印,这些打印演示了代码在client.execute(...)命令处停止响应。
该函数在try块内调用,在执行.execute(...)命令后,代码确实到达了finally块。
我已经搜索了类似的问题,但没有找到答案。
这是一个已知问题吗? 有谁知道会导致这种情况的原因吗? 或者我该如何解决?