Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4635698

HttpURLConnection.getResponseCode() thorws unexpected exception

XMLWordPrintable

    • hopper
    • sparc
    • solaris_8
    • Verified

      HttpURLConnection.getResponseCode() throws the following exception when response message set to blank on server side.

      java.io.FileNotFoundException: http://javacup.sfbay.sun.com:8080/examples/servlet/HttpErrStream
              at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:679)
              at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:1134)
              at
      sun.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1148)
              at HttpTestClient.main(HttpTestClient.java:22)

      This happens when blank error message with some error code has been sent from server side like this...
      response.sendError(HttpServletResponse.SC_NOT_FOUND, " ");



      Yingxian's comments on this:
      " comment begin.....
      I don't think the server side sends a " " following 404, if I add a trailing " "
      to the status line, everything works.

      According to the http RFC, the responsemessage can be empty, but the
      responsecode should be followed by a " ".

      Still it would be nicer for our http client to correctly handle the case when
      the responsecode is directly followed by CRLF. So file a bug, include our
      findings in the bug report so that whoever takes that bug would know what's
      going on.

        ........comment end"

      How to reproduce this:
      ======================

      Take the servlet and testclient code. I have executed this servlet on tomcat4.0 server and on client side merlin build 92.

      public class HttpTestClient {


              public static void main(String[] args) {

                      HttpURLConnection ucon = null;
                      int responseCode;

                      try {
      URL url = new URL("HTTP://javacup.sfbay.sun.com:8080/examples/servlet/HttpErrStream");

      // Replace this url with your server ...

                              ucon = (HttpURLConnection) url.openConnection();
      ucon.setDoInput(true);

                              ucon.connect();

      System.out.println(" Request method" + ucon.getRequestMethod());
                              System.out.println(" Response code " + ucon.getResponseCode());
      //System.out.println(" Content length" + ucon.getContentLength());
                              BufferedReader bin = new BufferedReader(new InputStreamReader(ucon.getErrorStream()));
                              String result1 = bin.readLine();
      bin.close();

      System.out.println("read message 1 from servlet" + result1);

                              ucon.disconnect();

                      }catch(Exception exp) {
                                      exp.printStackTrace();
                      }

              } // End of main

      } // End of class



      Servletcode :
      public class HttpErrStream extends HttpServlet {

          public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
          {
      try {
      System.out.println("IN HttpErrStream servlets do get");

               response.setContentType("text/html");

      StringBuffer errorMsg = new StringBuffer(" <html> <head> ");

      response.sendError(HttpServletResponse.SC_NOT_FOUND, " ");


           } catch(Exception exp) {
      exp.printStackTrace();
      }

         } // End of doGet
      }


      Other INformation:
      ====================

      From browser I am able to see the response code i.e. 404 in this case with the same servlet.



            alanb Alan Bateman
            vakellasunw Venkata Akella (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: