views:

66

answers:

1

I have a piece of Java code that opens a HTTP connection, writes data on that and then retrieves the response code. Since, the connectin was fine it should get HTTP_OK (i.e 200) but it is getting 1 in return.

This is baffling as 1 appears nowhere in the Http Response code specification. Could anyone throw some ideas on the potential problem area?

Following is the piece of code:

URL tempURL = new URL("http://www.google.com");

obj_URLHttpConnectionServlet = (HttpURLConnection)tempURL.openConnection();
obj_URLHttpConnectionServlet.setDoInput(true);
obj_URLHttpConnectionServlet.setDoOutput(true);

OutputStream obj_OutputStream = obj_URLHttpConnectionServlet.getOutputStream();

obj_OutputStream.write(sConfigurationData.getBytes());
obj_OutputStream.flush();
obj_OutputStream.close();
obj_OutputStream = null;

int iResponseCode = obj_URLHttpConnectionServlet.getResponseCode();
System.out.println("Response code received is : " + iResponseCode);

Output

Response code received is : 1

A: 

Because the server side has set it to 1. If this is under your control, then check/fix the code responsible for that. If this is outside your control, report/contact the site admin.


Update: As per the comments, Fiddler confirmed that the server side has set the first line of the response header to HTTP/200 1. That's plain weird. It should look more like HTTP/1.1 200. The part before the space should indicate the protocol/version and the part after the space should indicate the response code. This look more and more a server side issue. I'd contact the site admin.

BalusC
Is this possible for a server to alter the http resonse code? The same piece of code on other environment and for the same server is running fine and returning back 200
Tushu
In a servlet class you can easily do that by `response.setStatus(1)`. The webserver can also do that, the details depends on the make/version. To be sure, use a HTTP monitor tool like Fiddler to exclude the one or other (if it shows 1 as well, then it's the server, else it's the client).
BalusC
Ok, but still the big question remains that how can the same server return different response code for two same piece of codes (running in different environments)?
Tushu
This question can only be answered if the make/version is known and Fiddler has confirmed that it's coming from the server.
BalusC
Could it be that an in-between proxy returns the `1` in certain cases?
rsp
In theory, yes. With Fiddler you must be able to determine whether the problem is caused at the server (or proxy) side or the client side (your code).
BalusC
Using Fiddler for the http url I am getting following:RESPONSE CODES--------------HTTP/200: 1Does this mean that the server is returning 1 as the response code?
Tushu
That's plain weird. It should look more like `HTTP/1.1 200`. The part before the space should indicate the protocol/version and the part after the space should indicate the response code. Did you test other arbitrary websites as well? Like stackoverflow.com and google.com. This to exclude the probem being caused by a proxy.
BalusC