Bug in HttpURLConnection?

Discussions

Web tier: servlets, JSP, Web frameworks: Bug in HttpURLConnection?

  1. Bug in HttpURLConnection? (1 messages)

    A code like the following:

    HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8080/test/testservlet").openConnection();
    connection.connect();
    connection.getHeaderField(0);
    connection.getResponseCode();

    (cut-down for bravity) will bomb on IOException if the following are met:

    1. The result code is an error (e.g. sent from a servlet using HttpServletResponse.sendError(500)).
    2. The connection.getHeaderField(0) call above is commented out.

    I use JDK 1.2.2 which comes with JBuilder 3.5 on Windows 2000. The call to getHeaderField(0) is not required if the response code is OK.

    I tried to dig a bit in the JDK 1.2.2 source but couldn't see any evidence to this, I suspect that maybe it has to do with the way URL.openConnection() generates the actual URLConnection object (guess: using a factory?).

    Sorry for the slightly off-topic subject, it's still actually a code writen for a Servlet (yes, a servlet which connects as a client to other HTTP servers).
  2. Bug in HttpURLConnection?[ Go to top ]

    Hi, I have this sample code. Is for a servlet that runs as client of another servlet.



    URL url = new URL ("http://localhost/servlet/S1");
    HttpURLConnection httpConnection = (HttpURLConnection) url.getConnection();
    httpConnection.setDoOutput (true);
    httpConnection.setRequestMethod ("POST"); // I could be GET

    PrintWriter pw = new printWriter (httpConnection.getOutputStream ());
    pw.print ("key1=value1"); // I send this variable in the request
    pw.flush ();

    Object nextPage = httpConnection.getContent();

    out.println ("Response Message = " + httpConnection.getResponseCode());

    // If getResponseCode() returns 200, the HTTP communication was OK.
    // After this, you will treat the response

    InputStream is = httpConnetcion.getInputStream ();
    InputStreamReader reader = new InputStreamReader (is);
    BufferedReader stringReader = new BufferedReader (reader);

    out.println ("Received...");
    while (stringReader.ready())
    {
        String line = stringReader.readLine();
        out.println (line);
    }


    Is important to set the HTTP request method , flush the OutputStream and get the content of response. Is not mandatory to send variables.

    Salu2