You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Yueming Xu <yx...@vitria.com> on 2002/02/12 23:58:58 UTC

Cannot get HTTP response code

Hi, 

I have a servlet running in Tomcat, and it sometimes returns error by calling HttpServletResponse.sendError(400, "Invalid request").

The following code works with Tomcat 4.0, it can display both regular response (status 200) and error response (status 400.)  However, in Tomcat 3.2.1, I get an error: 

java.net.ProtocolException: Can't reset method: already connected
        at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:106)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:405)

So, I had to remove the call connection.connect() for the regular 200 response to go through.  However, the 400 error response will cause the following exception:

java.io.FileNotFoundException: http://localhost:8080/servlet/simple
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:529)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:145)

What is the problem of this code?  Is this a bug in Tomcat 3.x, or is it an HTTP version issue?

Thanks,
Yueming

[Code snipet ...]

connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(true);

// this line is the solution and problem
connection.connect();

// send request
PrintWriter out = new PrintWriter( connection.getOutputStream() );
out.println(stringToSend);
out.close();

// read response
int status = connection.getResponseCode();
System.out.println("\nResponse Code: " + status);
System.out.println("Response Message: " + connection.getResponseMessage());