编程语言
首页 > 编程语言> > 调用接口异常 java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOut

调用接口异常 java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOut

作者:互联网

异常全文:java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true) 

异常分析:

  1、发起GET请求,请求的HttpEntity中包含body参数。

    解决方案:改变声明HttpEntity的方式——

        httpEntity = new HttpEntity<>(restUtilParams.getBody(), httpHeaders);
    ==> HttpEntity httpEntity = new HttpEntity<>(httpHeaders);
       ResponseEntity<String> responseEntity =
       templateMap.get(TEMPLATE).exchange(uri, HttpMethod.GET, httpEntity, String.class);

  2、URLConnection发起POST请求少设置参数

    解决方案:为URLConnection 添加两参数——

      URLConnection conn = url.openConnection();
      conn.setDoOutput(true);  
          conn.setDoInput(true);

标签:URLConnection,HttpEntity,java,httpEntity,setDoOutput,true,conn
来源: https://www.cnblogs.com/minaotor/p/15100252.html