java - 使用 DefaultHttpClient 尝试 POST 请求时连接重置
问题描述
我有一个从 java 后端代码中的服务调用获得的字符串值。当我得到一个字符串的值时,我想发出一个 post 请求并将字符串作为 url 编码的 base 64 字符串传递给端点。
问题是我得到一个 Socket Exception: Connection reset 错误。我正在使用 HttpClient 4.5.2 和 IBM Websphere (Rational Application Development) 更详细的错误是:
DefaultHttpClient org.apache.http.impl.client.DefaultRequestDirector tryConnect I/O Exception (java.net.SocketException) caught when connecting to {s} -> https://foo.bar:443: Connection reset
Java代码:
public ActionForward executeShowFormView(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
var stringResponse = serviceCall.getStringResponse();
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serviceCall.getEndpointUrl());
StringEntity entity = new StringEntity("response=" + stringResponse, ContentType.APPLICATION_TYPE_URLENCODED);
post.setEntity(entity);
HttpResponse response1 = client.execute(post);
}
我能做些什么来修复错误?
解决方案
就我而言,我使用了 httpclient post 方法。我将请求标头值设置为
post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1").
我的应用程序已投入生产并成功运行了 1 年。突然它停止工作,我收到 javax.net.ssl.SSLProtocolException: Connection reset
使固定:
我变了post.setRequestHeader("Content-Type", "application/xml")
这解决了我的问题。
推荐阅读
- kendo-ui - 如果不存在数据,则 Kendo Multiselect 删除所有选项
- python - TypeError:bool_ 类型的对象不是 JSON 可序列化的
- entity-framework-core - EF-core 如何建立与“自身”的多对多关系
- javafx - 是否可以按类别显示 ComboBox 项目?
- laravel - 如何发送从 DB 获取值的自动 pdf 邮件
- java - 如何测试 PKI 签名验证算法
- powershell - Powershell 引用变量值
- ios - 根据自动布局更改 UILabel 中的字体大小(swift)
- magento-2.3 - 在 magento2 的购物车页面将商品添加到购物车时,您的会话已过期
- monitoring - Prometheus 监控错误“度量后的预期值,得到目标端点的“MNAME””