请求http接口报错:ResourceAccessException: xxx cannot retry due to server authentication, in streaming mode


前言

使用RestTemplate请求外部接口时报错


代码

public static GetUserInfoResp getUserInfo(String authToken) {
	try {
		HttpEntity<String> formEntity = new HttpEntity<>(new JSONObject().toJSONString(), getHttpHeaders(authToken));
		JSONObject resp = restTemplate.postForObject(mallConfig.getGetUserInfo(), formEntity, JSONObject.class);
		log.info("getUserInfo请求结果为:" + JSONObject.parseObject(JSON.toJSONString(resp)));
		return resp == null ? null : JSONObject.parseObject(resp.toJSONString(), GetUserInfoResp.class);
	} catch (Exception e) {
		log.error("getUserInfo出现异常:" + e.getMessage(), e);
		return null;
	}
}

报错信息

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://10.200.199.100/auth/getUserInfo": cannot retry due to server authentication, in streaming mode; nested exception is java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:791)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717)
	at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:443)
	at com.xxx.huarunshouzheng.util.MallInterfaceUtil.getUserInfo(MallInterfaceUtil.java:212)
	at com.xxx.huarunshouzheng.controller.MallController.getUserInfo(MallController.java:177)
	at com.xxx.huarunshouzheng.controller.MallController$$FastClassBySpringCGLIB$$c2e535de.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:792)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
	at com.xxx.aop.FrequencyAspect.around(FrequencyAspect.java:66)
	at sun.reflect.GeneratedMethodAccessor175.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:707)
	at com.xxx.huarunshouzheng.controller.MallController$$EnhancerBySpringCGLIB$$6f271e42.getUserInfo(<generated>)
	at sun.reflect.GeneratedMethodAccessor294.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:515)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:583)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:212)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:156)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:181)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:156)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:181)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:156)
	at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:126)
	at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:64)
	at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:101)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
	at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:181)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:156)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:181)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:156)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:168)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:483)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:679)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:617)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:934)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1698)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:750)
Caused by: java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1700)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1500)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:55)
	at org.springframework.web.client.MessageBodyClientHttpResponseWrapper.getRawStatusCode(MessageBodyClientHttpResponseWrapper.java:130)
	at org.springframework.web.client.MessageBodyClientHttpResponseWrapper.hasMessageBody(MessageBodyClientHttpResponseWrapper.java:61)
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:784)
	... 76 common frames omitted

排查

1、查看日志,发现程序走到下面这一行就出现报错,连响应结果都无法封装

JSONObject resp = restTemplate.postForObject(mallConfig.getGetUserInfo(), formEntity, JSONObject.class);

2、分析日志cannot retry due to server authentication, in streaming mode发现报错与有关

但该接口是用JSONObject来接收响应结果的,为什么会报与流相关的错误?

除非该接口返回的数据类型不是application/json


3、由于环境原因,该接口无法debug,所以通过postman调用接口,查看响应上下文

发现接口返回类型不是application/json,而是text/html;charset=UTF8

在这里插入图片描述


在这里插入图片描述


原因

接口返回类型不是application/json,而是text/html;charset=UTF8


根据Java官方文档,java.net.HttpRetryException异常在以下情况下抛出:

HTTP协议需要重新发送一个请求,但无法自动执行。这通常发生在接收到某些特定的HTTP响应码(如307 Temporary Redirect和308 Permanent Redirect)但当前处于流模式发送数据时。

HTTP服务器要求认证。当服务器返回401 Unauthorized或者407 Proxy Authentication Required响应,并且缺失有效的认证信息时,也会抛出此异常。

在这本例中,由于返回的是一个html页面,所以HttpURLConnection对象试图自动处理HTTP重定向,但由于正在流模式(streaming mode)下发送数据,所以不能重新发送请求


解决

更改接口的响应类型为:application/json

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
When you encounter a "ProxyError: Conda cannot proceed due to an error in your proxy configuration" message, it means that there is an issue with the proxy settings in your Conda configuration. Conda uses the proxy settings to connect to the internet and download packages. To resolve this issue, you can try the following steps: 1. Check your proxy settings: Verify that the proxy settings in your Conda configuration are correct. You can do this by running the following command in your terminal: ``` conda config --show | grep proxy ``` Make sure that the proxy settings displayed are accurate and match your network configuration. 2. Update proxy settings: If you need to update your proxy settings, you can use the following commands to set the proxy configuration for Conda: ``` conda config --set proxy_servers.http <proxy_url> conda config --set proxy_servers.https <proxy_url> ``` Replace `<proxy_url>` with the URL of your proxy server. 3. Remove proxy settings: If you don't require a proxy or want to remove the existing proxy settings, you can use the following commands: ``` conda config --remove-key proxy_servers.http conda config --remove-key proxy_servers.https ``` 4. Set environment variables: In some cases, you may need to set environment variables for your proxy configuration. You can do this by exporting the `HTTP_PROXY` and `HTTPS_PROXY` variables in your terminal: ``` export HTTP_PROXY=<proxy_url> export HTTPS_PROXY=<proxy_url> ``` Replace `<proxy_url>` with the URL of your proxy server. 5. Retry Conda operations: After updating or removing the proxy settings, try running your Conda commands again. The ProxyError should no longer occur if the proxy configuration is resolved. If these steps don't resolve the issue, you may need to consult your network administrator or IT support for further assistance with your proxy configuration.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值