java.io.IOException:服务器返回 HTTP 响应代码:500

发布于 2024-09-13 07:41:47 字数 861 浏览 6 评论 0原文

我在使用 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)

出了什么问题?楼主屏蔽我了吗?

I'm facing this problem with Java. I want to get some HTML informations from a URL. This code was working for so long, but suddenly, it stopped working.

When I access this URL using the browser, it opens with no problem.

The code:

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();

The Exception:

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)

What's wrong? Did the host block me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

嘿看小鸭子会跑 2024-09-20 07:41:47

HTTP 状态代码 500 通常表示网络服务器代码已坠毁了。您需要预先使用 HttpURLConnection#getResponseCode(),如果出现错误,请阅读 HttpURLConnection#getErrorStream() 代替。它可能包含有关问题的信息。

如果主机阻止了您,您宁愿获得 401 或 403 等 4nn 状态代码。

另请参阅:

HTTP status code 500 usually means that the webserver code has crashed. You need to determine the status code beforehand using HttpURLConnection#getResponseCode() and in case of errors, read the HttpURLConnection#getErrorStream() instead. It may namely contain information about the problem.

If the host has blocked you, you would rather have gotten a 4nn status code like 401 or 403.

See also:

水晶透心 2024-09-20 07:41:47

此状态代码 500 是内部服务器错误。此代码表示服务器的一部分(例如,CGI 程序)已崩溃或遇到配置错误。

我认为问题不在你这边,而在Http服务器那边。
您用来访问的资源可能已被移动或损坏,或其配置可能已更改或损坏

This Status Code 500 is an Internal Server Error. This code indicates that a part of the server (for example, a CGI program) has crashed or encountered a configuration error.

i think the problem does'nt lie on your side, but rather on the side of the Http server.
the resources you used to access may have been moved or get corrupted, or its configuration just may have altered or spoiled

寄意 2024-09-20 07:41:47

我遇到了这个问题,即粘贴到浏览器中时工作正常,但通过 java 完成时则为 505。这只是需要转义/编码的空格。

I had this problem i.e. works fine when pasted into browser but 505s when done through java. It was simply the spaces that needed to be escaped/encoded.

说谎友 2024-09-20 07:41:47

问题一定出在您传递的参数上(您必须传递空白参数)。例如: http://www.myurl.com?id=5&name=
检查您是否正在调用的服务器上处理此问题。

The problem must be with the parameters you are passing(You must be passing blank parameters). For example : http://www.myurl.com?id=5&name=
Check if you are handling this at the server you are calling.

可是我不能没有你 2024-09-20 07:41:47

将内容类型更改为“application/x-www-form-urlencoded”,我解决了问题。

Change the content-type to "application/x-www-form-urlencoded", i solved the problem.

节枝 2024-09-20 07:41:47

您可以查看第一个服务器响应并查看服务器是否向您发送了 cookie。

要检查服务器是否向您发送了 cookie,您可以使用 HttpURLConnection#getHeaderFields() 并查找名为“Set-Cookie”的标头。

如果存在,这是您问题的解决方案。 100%为这个案子而努力!

You may look within the first server response and see if the server sent you a cookie.

To check if the server sent you a cookie, you can use HttpURLConnection#getHeaderFields() and look for headers named "Set-Cookie".

If existing, here's the solution for your problem. 100% Working for this case!

蹲在坟头点根烟 2024-09-20 07:41:47

就我而言,我已将内容类型更改为接受,它解决了问题。

    URL url = new URL(GET_URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("Accept", "application/json; charset=utf-8");

In my case, I had changed the Content-Type to Accept and it resolved the issue.

    URL url = new URL(GET_URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("Accept", "application/json; charset=utf-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文