java.io.IOException:服务器返回 HTTP 响应代码 505
我的代码中有基于 HTML 的查询,并且一种特定类型的查询似乎在收到服务器的 505 响应后会引发 IOExceptions。我和其他似乎有类似问题的人一起查看了 505 回复。显然505代表HTTP版本不匹配,但是当我将相同的查询URL复制到任何浏览器(尝试过firefox、seamonkey和Opera)时似乎没有问题。我读过的一篇文章建议浏览器可能会自动处理版本不匹配的问题。
我尝试使用 Opera 附带的不错的开发工具进行更深入的研究,看起来版本没有不匹配的情况(我相信Java 使用 HTTP 1.1)并收到良好的 200 OK
响应。为什么当相同的查询通过我的 Java 代码时会遇到问题?
private InputStream openURL(String urlName) throws IOException{
URL url = new URL(urlName);
URLConnection urlConnection = url.openConnection();
return urlConnection.getInputStream();
}
示例链接:http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length
I have HTML based queries in my code and one specific kind seems to give rise to IOExceptions
upon receiving 505 response from the server. I have looked up the 505 response along with other people who seemed to have similar problems. Apparently 505 stands for HTTP version mismatch, but when I copy the same query URL to any browser (tried firefox, seamonkey and Opera) there seems to be no problem. One of the posts I read suggested that the browsers might automatically handle the version mismatch problem..
I have tried to dig in deeper by using the nice developer tool that comes with Opera, and it looks like there is no mismatch in versions (I believe Java uses HTTP 1.1) and a nice 200 OK
response is received. Why do I experience problems when the same query goes through my Java code?
private InputStream openURL(String urlName) throws IOException{
URL url = new URL(urlName);
URLConnection urlConnection = url.openConnection();
return urlConnection.getInputStream();
}
sample link: http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Tomcat 中存在一些 URL 中包含空格的问题。要解决此问题,您需要使用
URLEncoder
对您的 url 进行编码。示例(注意空格):
There has been some issues in Tomcat with URLs containing space in it. To fix the problem, you need to encode your url with
URLEncoder
.Example (notice the space):
作为 www.uniprot.org 的开发人员,我的优势是能够查看请求日志。根据日志,去年我们没有发送 505 响应代码。无论如何,我们的服务器确实可以理解 http 1 请求以及默认的 http1.1(尽管您可能不会得到您期望的结果)。
这让我怀疑是否存在某种数据损坏。或者您受到硬件故障的影响(最近我们在交换机和整个数据中心方面遇到了一些麻烦;)。无论如何,如果您对 uniprot.org 有任何疑问或问题,请联系 [电子邮件受保护] 然后我们可以看看是否可以帮助/解决问题。
您的代码片段看起来很正常并且应该可以工作。
问候,
杰文·博勒曼
AS a developer at www.uniprot.org I have the advantage of being able to look in the request logs. In the last year according to the logs we have not send a 505 response code. In any case our servers do understand http 1 requests as well as the default http1.1 (though you might not get the results that you expect).
That makes me suspect there was either some kind of data corruption on the way. Or you where affected by a hardware failure (lately we have had some trouble with a switch and a whole datacentre ;). In any case if you ever have questions or problems with uniprot.org please contact [email protected] then we can see if we can help/fix the problem.
Your code snippet seems normal and should work.
Regards,
Jerven Bolleman
你是在代理后面吗?这段代码对我有用,并打印出我通过浏览器看到的相同文本。
conn
是 的实例HttpURLConnectionAre you behind a proxy? This code works for me and prints out the same text I see through a browser.
conn
is an instance of HttpURLConnection来自 URL 类的 API 文档:
因此,如果 url-str 中有任何空格,请在调用 new URL(url-str) 之前对其进行编码
from the API documentation for the URL class:
so if you have any spaces in your url-str encode it before calling new URL(url-str)
@posdef 我遇到了相同的 HTTP 错误代码 505 问题。当我将在 Java 代码中使用的 URL 粘贴到 Firefox 和 Chrome 中时,它起作用了。但通过代码给出了 IOException。但最后我发现在 url 字符串中有括号“(”和“)”,通过删除它们它可以工作,所以看来我需要像浏览器一样进行 URLEncodeing。
@posdef I was having same HTTP error code 505 problem. When I pasted URL that I was using in Java code in Firefox, Chrome it worked. But through code was giving IOException. But at last I came to know that in url string there were brackets '(' and ')', by removing them it worked so it seems I needed URLEncodeing same like browsers.