0 Replies Latest reply on Jul 25, 2005 8:39 AM by Nitin Jain

    Getting 400 response code while  HttpURLConnection.getRespon

    Nitin Jain Newbie

      Hi There,

      I'm getting 400 bad request whenever I try to use HttpURLConnection in my code. Moreover, the server is not recieving any request.

      URL url = new URL("http", serverName, portNo, script);
      HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
      urlConn.setDoInput(true);
      urlConn.setDoOutput(true);
      urlConn.setRequestMethod("POST");
      FileInputStream fileInputStream = new FileInputStream(fileName);
      File file = new File(fileName);
      int contentLength = (int) file.length();
      urlConn.setRequestProperty("Content-Length", "" + contentLength);
      OutputStream outputStream = urlConn.getOutputStream();
      int bytesRead = 0;
      byte[] byteData = new byte[51200];
      while((bytesRead = fileInputStream.read(byteData, 0, byteData.length)) != -1){
      outputStream.write(byteData, 0, bytesRead);
      contentLength += bytesRead;
      }
      outputStream.flush();
      outputStream.close();
      fileInputStream.close();
      }
      responseCode = urlConn.getResponseCode();
      System.out.println(responseCode);
      


      The output of this code is always 400 and server never recieved any request.

      Thanks in advance.

      Regards,
      Nitin