java.io.IOException:服务器返回 HTTP 响应代码:500
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3432263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.IOException: Server returned HTTP response code: 500
提问by rlc
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.
我正面临 Java 的这个问题。我想从 URL 中获取一些 HTML 信息。这段代码工作了很长时间,但突然间,它停止工作了。
When I access this URL using the browser, it opens with no problem.
当我使用浏览器访问这个 URL 时,它打开没有问题。
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?
怎么了?楼主屏蔽我了吗?
采纳答案by BalusC
HTTP status code 500usually 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.
HTTP 状态代码 500通常意味着网络服务器代码已崩溃。您需要在使用之前确定状态代码HttpURLConnection#getResponseCode()
,如果出现错误,请HttpURLConnection#getErrorStream()
改为阅读。它可以即包含关于问题的信息。
If the host has blocked you, you would rather have gotten a 4nn status code like 401 or 403.
如果主机阻止了您,您宁愿获得 4nn 状态代码,例如 401 或 403。
See also:
也可以看看:
回答by arthur
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.
此状态代码 500 是内部服务器错误。此代码表示服务器的一部分(例如,CGI 程序)已崩溃或遇到配置错误。
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
我认为问题不在于您,而在于 Http 服务器。您用于访问的资源可能已被移动或损坏,或者其配置可能已更改或损坏
回答by AMIL.Y
I have encountered the same problem and found out the solution.
我遇到了同样的问题并找到了解决方案。
You may look within the first server response and see if the server sent you a cookie.
您可以查看第一个服务器响应,看看服务器是否向您发送了 cookie。
To check if the server sent you a cookie, you can use HttpURLConnection#getHeaderFields() and look for headers named "Set-Cookie".
要检查服务器是否向您发送了 cookie,您可以使用 HttpURLConnection#getHeaderFields() 并查找名为“Set-Cookie”的标头。
If existing, here's the solution for your problem.100% Working for this case!
如果存在,这里是您问题的解决方案。100% 为这个案例工作!
+1 if it worked for you.
+1 如果它对你有用。
回答by GarethReid
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.
我有这个问题,即粘贴到浏览器时工作正常,但通过 java 完成时为 505s。这只是需要转义/编码的空格。
回答by hongjunzhan
Change the content-type to "application/x-www-form-urlencoded", i solved the problem.
将内容类型更改为“application/x-www-form-urlencoded”,我解决了问题。
回答by Prem
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.
问题一定出在您传递的参数上(您必须传递空白参数)。例如:http://www.myurl.com?id=5&name=检查您是否在正在调用的服务器上处理此问题。