为什么 HttpURLConnection.getResponseCode() 返回 1?
我有一段 Java 代码,它打开一个 HTTP 连接,在其上写入数据,然后检索响应代码。因为连接没问题,它应该得到 HTTP_OK(即 200),但它得到的回报是 1。
这令人困惑,因为 1 没有出现在 Http 响应代码规范中。有人可以对潜在问题领域提出一些想法吗?
以下是一段代码:
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);
输出
收到的响应代码是: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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为服务器端已经将其设置为
1
。如果这是在你的控制之下,那么检查/修复负责的代码。如果这超出了您的控制范围,请报告/联系网站管理员。更新:根据评论,Fiddler确认服务器端已将响应标头的第一行设置为
HTTP/200 1
。这实在是太奇怪了。它应该看起来更像HTTP/1.1 200
。空格之前的部分应指示协议/版本,空格之后的部分应指示响应代码。这看起来越来越像是服务器端问题。我会联系网站管理员。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 likeHTTP/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.