我正面临 Java 的这个问题。我想从 URL 获取一些 HTML 信息。这段代码工作了很长时间,但突然间,它停止工作了。
当我使用浏览器访问此 URL 时,它可以毫无问题地打开。
编码:
URL site = new URL(this.url);
java.net.URLConnection yc = site.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String objetivo = "<td height=\"28\" colspan=\"2\"";
while ((inputLine = in.readLine()) != null && !inputLine.contains(objetivo)) {
}
inputLine = in.readLine();
例外:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.myurl.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at Sites.websites.Site1.getData(Site1.java:53)
at util.Util.lerArquivo(Util.java:278)
at util.Util.main(Util.java:983)
怎么了?楼主屏蔽我了吗?
原文由 rlc 发布,翻译遵循 CC BY-SA 4.0 许可协议
HTTP 状态代码 500 通常意味着网络服务器代码已崩溃。您需要使用
HttpURLConnection#getResponseCode()
预先确定状态代码,如果出现错误,请阅读HttpURLConnection#getErrorStream()
代替。它可能包含有关问题的信息。如果主机阻止了你,你宁愿得到一个 4nn 状态代码,如 401 或 403。
也可以看看: