首页   >   代码编程

发送http请求,报错:Cannot write output after reading input.

最近对接一个第三方,有专门的系统来对接,我们直接对接内部的系统即可,但是调用接口时,发现一直报错:Cannot write output after reading input,大概意思是不能在读取了输入流之后去写输出流。

起初还以为是对接第三方报错了,仔细检查一番,竟然是同事的代码写的有问题,开启了两次输出流

错误信息如下:

2019-03-12 11:51:03,717 ERROR TongDunManager: [ApiInvoker] invoke throw exception, details: java.net.ProtocolException: Cannot write output after reading input.
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1330)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
        at com.st.gateway.project.biz.tongdun.TongDunManager.invoke(TongDunManager.java:175)
        at com.st.gateway.project.biz.tongdun.TongDunManager.ktpTaskQuery(TongDunManager.java:99)
        at com.st.gateway.project.web.rest.TongDunCallbackController.lambda$ktp$0(TongDunCallbackController.java:32)
        at com.st.gateway.common.core.biz.RestBusinessTemplate.doExecute(RestBusinessTemplate.java:66)
        at com.st.gateway.common.core.biz.RestBusinessTemplate.execute(RestBusinessTemplate.java:23)
        at com.st.gateway.project.web.rest.TongDunCallbackController.ktp(TongDunCallbackController.java:27)
        at com.st.gateway.project.web.rest.TongDunCallbackController$FastClassBySpringCGLIB$78ded699.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
        at com.st.gateway.common.base.aop.ApiDigestInterceptor.invoke(ApiDigestInterceptor.java:31)
        at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

错误代码片段:

conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setConnectTimeout(1000);
conn.setReadTimeout(3000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.getOutputStream().write(postBody.toString().getBytes());
conn.getOutputStream().flush();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
    return null;
}

错误的地方,我用红色框起来

发送http请求,报错:Cannot write output after reading input.

在开始没看代码之前,查阅了一些资料,资料中也有类似的问题,比如:在getOutputStream()之前执行getResponseCode(),这样也会有同样的错误。

更改之后的代码如下:

conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(postBody.toString().getBytes());
os.flush();
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
    return null;
}

QQ群Ⅰ: 686430774 (已满)

QQ群Ⅱ: 718410762 (已满)

QQ群Ⅲ: 638620451 (已满)

QQ群Ⅳ: 474195684

如果文章有帮到你,可以考虑请博主喝杯咖啡!

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2019-03-12

永久地址:https://www.jiweichengzhu.com/article/4bdd57575da84d8cad5dc1e4f5a1f4ca

评论

相关阅读