java - HTTP post 请求在生产环境中没有响应(与 tomcat 服务器的战争)
问题描述
我已经实现了一个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
块。
我已经搜索过类似的问题,但没有找到答案。
这是一个已知问题吗?有谁知道是什么原因导致的?或者我该如何解决?
解决方案
推荐阅读
- python-3.x - 通过 API IB 下订单
- django - Django 表单创建默认用户而不是自定义用户。如何创建自定义用户?
- java - Dialog 和方向更改后更新 RecyclerView
- javascript - 如何设置 TinyMCE 边框半径?
- security - 将用户上传的文件发送到远程服务器的传输协议?
- php - 如何修复 PHP 7.2 explode() 至少需要 2 个参数,其中 1 个在
- api-platform.com - 如何在 api-platform 中“验证”DELETE 请求
- sql - 需要 powershell 脚本从 T-SQL 压缩文件
- apache - Moodle 3.7 & Apache & 反向代理结果 ERR_TOO_MANY_REDIRECTS
- angular - Ionic Firebase CRUD 应用程序无法匹配任何路由