java.net.HttpRetryException:在流模式下由于服务器身份验证而无法重试 [英] java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode

查看:1812
本文介绍了java.net.HttpRetryException:在流模式下由于服务器身份验证而无法重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序包含两个部分:
服务器-提供REST服务
客户端-通过Spring restTemplate消费它们

We have two parts in our app:
Server - provide REST services
Client - consume them via Spring restTemplate

除了HTTP状态外,我们的服务器还返回带有JSON的HTTP正文,其中详细描述了错误. 因此,我在restTemplate中添加了自定义错误处理程序,以将某些错误编码为非错误-它有助于很好地解析HTTP正文.

In addition to the HTTP status our server returns an HTTP body with JSON that describe error in detail. So, I've added custom Error handler to restTemplate to treat some error coded as non errors - it helps parse HTTP body very well.

但是在HTTP/1.1 401未经授权的情况下,我通过解析HTTP正文得到了异常. 其他所有错误代码都可以正确处理(400、402等) 我们使用的是普通服务器逻辑,在发生错误的情况下会发送HTTP响应,对于不同类型的错误,没有特殊的规则:

But I get an exception via parsing of the HTTP body in the case of an HTTP/1.1 401 Unauthorized. All other error codes are handled fine(400, 402, etc. ) We are using plain server logic that sends HTTP response in the case of an error, no special rules for different types of an error:

writeErrorToResponse(int status, String errMsg, HttpServletResponse resp) throws IOException {
        response.setStatus(status);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        String message = String.format("{\"error\":\"%s\"}", StringUtils.escapeJson(errMsg));
        resp.getWriter().println(message);
    }

但是在客户端上,只有HTTP/1.1 401会引发异常-"java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode"

But on client only HTTP/1.1 401 throws exception - "java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode"

我进行了一些调试,发现问题的原因是SimpleClientHttpResponse中的代码:

I've made some debugging and see that the cause of the problem is code in SimpleClientHttpResponse:

HttpURLConnection.getInputStream()

HttpURLConnection.getInputStream()

与Fiddler进行追踪有以下一些应对措施: 邮件在客户端上解析正确:

Tracing with Fiddler have these next responses: Message is parsed correct on the client:

HTTP/1.1 402 Payment Required
X-Powered-By: Servlet/3.0
Content-Type: application/json
Content-Language: en-GB
Content-Length: 55
Connection: Close
Date: Sat, 25 May 2013 10:10:44 GMT
Server: WebSphere Application Server/8.0

{"error":"I cant find that user.  Please try again."}

以及引起异常的消息:

HTTP/1.1 401 Unauthorized
X-Powered-By: Servlet/3.0
Content-Type: application/json
Content-Language: en-GB
Content-Length: 55
Date: Sat, 25 May 2013 11:00:21 GMT
Server: WebSphere Application Server/8.0

{"error":"I cant find that user.  Please try again."}

在这种情况下,java.net.HttpRetryException可能是什么原因?

What could be the cause of java.net.HttpRetryException in this situation?

另外:以前,该机制还可以正常工作.但是,由于我们在应用程序中更改了很多代码.

In addition: Some times ago this mechanism worked fine. But since we have changed a lot of code in app.

推荐答案

使用问题是由于在身份验证的情况下进行了分块和随后的重试机制.

The problem is due to chunking and subsequent retry mechanism incase of authentication.

您还可以使用HttpClientPolicy禁用块

You can also disable chunks using HttpClientPolicy

这篇关于java.net.HttpRetryException:在流模式下由于服务器身份验证而无法重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆