调用接口异常 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);
上一篇:java的HTTPS请求绕过证书


下一篇:一个jmeter自动化问题(对图片进行Base64、下载图片并保存到本地)