为什么 HttpURLConnection.getResponseCode() 返回 1?

发布于 2024-09-18 05:20:19 字数 829 浏览 7 评论 0原文

我有一段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2024-09-25 05:20:19

因为服务器端已经将其设置为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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文