調用接口異常 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