【问题标题】:Getting error 502/504 when trying to get InputStream尝试获取 InputStream 时出现错误 502/504
【发布时间】:2013-12-16 08:25:25
【问题描述】:
URL queryUrl = new URL(url);
InputStream inputStream = null;
HttpsURLConnection connection = (HttpsURLConnection) queryUrl.openConnection();
connection.setRequestProperty("User-Agent", "My Client");
connection.setRequestMethod("GET"); 
connection.setDoInput(true);
inputStream = connection.getInputStream();

嗨, 我正在使用上面的代码来执行 HttpGet 查询。 我在几次尝试中遇到了服务器返回错误代码 502 或 504 的异常(两种情况都会发生)。 在行中抛出异常:

inputStream = connection.getInputStream()

有什么想法吗?

您的帮助将不胜感激。 提前致谢

【问题讨论】:

  • 什么类型的异常?服务器日志在说什么?
  • 你会发布错误 StackTrace 吗!
  • 服务器返回 HTTP 响应代码:504 用于 URL:....... sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) sun.net.www.protocol。 https.HttpsURLConnectionImpl.getInputStream(未知来源) Utils.queryGET(BtcE.java:330)

标签: java http get


【解决方案1】:

5xx 中的错误代码表示服务器或代理存在问题。 RFC 声明

Response status codes beginning with the digit "5" indicate cases in which the server is 
aware that it has erred or is incapable of performing the request. Except when responding 
to a HEAD request, the server SHOULD include an entity containing an explanation of the 
error situation, and whether it is a temporary or permanent condition. User agents SHOULD
display any included entity to the user. These response codes are applicable to any request method.

请通过阅读以下 url 连接的错误流来检查实际错误是什么: 如果 HTTP 响应代码是 4nn(客户端错误)或 5nn(服务器错误),那么您可能需要阅读 HttpURLConnection#getErrorStream() 以查看服务器是否发送了任何有用的错误信息。

InputStream error = ((HttpURLConnection) connection).getErrorStream();   

我还认为,对于 http 请求,您可以使用 Apache HttpClient 而不是直接使用 HttpUrlConnection。使用 HttpClient 更容易。

【讨论】:

  • 感谢您的回复,我会将errorCode输出添加到我的代码中并尝试分析。与此同时 - 我在那里做错了什么会导致异常吗?
  • 由于错误是状态码 5xx,我怀疑来自客户端(您的)端的任何问题。在您收到 5xx 响应期间,请检查 Web 服务器日志和代理日志以获取任何线索。
猜你喜欢
  • 2019-02-23
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 2014-12-14
  • 2021-10-01
  • 1970-01-01
  • 2022-06-23
相关资源
最近更新 更多