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


异常全文: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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM