FileNotFoundException while getting the InputStream object from HttpURLConnection

前端 未结 7 2168
清酒与你
清酒与你 2020-11-29 02:47

I am trying to send a post request to a url using HttpURLConnection (for using cUrl in java). The content of the request is xml and at the end point, the application proces

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 03:09

    For anybody else stumbling over this, the same happened to me while trying to send a SOAP request header to a SOAP service. The issue was a wrong order in the code, I requested the input stream first before sending the XML body. In the code snipped below, the line InputStream in = conn.getInputStream(); came immediately after ByteArrayOutputStream out = new ByteArrayOutputStream(); which is the incorrect order of things.

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // send SOAP request as part of HTTP body 
    byte[] data = request.getHttpBody().getBytes("UTF-8");
    conn.getOutputStream().write(data); 
    
    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
      Log.d(TAG, "http response code is " + conn.getResponseCode());
      return null;
    }
    
    InputStream in = conn.getInputStream();
    

    FileNotFound in this case was an unfortunate way to encode HTTP response code 400.

提交回复
热议问题