【问题标题】:Connection Reset when trying a POST request using DefaultHttpClient使用 DefaultHttpClient 尝试 POST 请求时连接重置
【发布时间】:2019-02-10 03:21:20
【问题描述】:

我有一个字符串值,它是从我的 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); 
}

我可以做些什么来修复错误?

【问题讨论】:

  • 你是否尝试过从邮递员到同一台服务器的相同请求
  • @vinaychhabra 是的。在邮递员中,我收到 500 内部服务器错误。但是,即使对于那个端点,我认为这可能是一个问题。我尝试使用获取请求访问 google.com,但我仍然会重置连接,即使在另一个使用获取请求访问 google.com 的空白项目中,它工作得很好。

标签: java jsp struts apache-commons-httpclient connection-reset


【解决方案1】:

在我的例子中,我使用了 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")

这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2017-02-16
    • 2021-04-19
    • 1970-01-01
    • 2014-04-15
    • 2015-05-11
    相关资源
    最近更新 更多