简体   繁体   中英

Why is HttpURLConnection.getResponseCode() returning 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 (ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM