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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625659/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:49:58  来源:igfitidea点击:

java.io.IOException: Server returns HTTP response code 505

javahttpversionioexceptionhttp-status-code-505

提问by posdef

I have HTML based queries in my code and one specific kind seems to give rise to IOExceptionsupon 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..

我的代码中有基于 HTML 的查询,并且IOExceptions在收到来自服务器的 505 响应时似乎会产生一种特定的查询。我和其他似乎有类似问题的人一起查找了 505 响应。显然 505 代表 HTTP 版本不匹配,但是当我将相同的查询 URL 复制到任何浏览器(尝试过 firefox、seamonkey 和 Opera)时,似乎没有问题。我读过的一篇文章建议浏览器可能会自动处理版本不匹配问题。

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 OKresponse is received. Why do I experience problems when the same query goes through my Java code?

我试图通过使用 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();
 }

sample link: http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length

示例链接: http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length

采纳答案by Jerven

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.

Tomcat 中存在一些问题,其中 URL 中包含空格。要解决此问题,您需要使用URLEncoder.

Example (notice the space):

示例(注意空格):

String url="http://example.org/test test2/index.html";
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
System.out.println(encodedURL); //outputs http%3A%2F%2Fexample.org%2Ftest+test2%2Findex.html

回答by Jon Freedman

Are you behind a proxy? This code works for me and prints out the same text I see through a browser.

你在代理后面吗?此代码适用于我并打印出我通过浏览器看到的相同文本。

final URL url = new URL("http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length");
final URLConnection conn = url.openConnection();
final InputStream is = conn.getInputStream();
System.out.println(IOUtils.toString(is));

connis an instance of HttpURLConnection

connHttpURLConnection 的一个实例

回答by Jerven

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).

作为 www.uniprot.org 的开发人员,我的优势是能够查看请求日志。根据日志,去年我们没有发送 505 响应代码。在任何情况下,我们的服务器都可以理解 http 1 请求以及默认的 http1.1(尽管您可能不会得到您期望的结果)。

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.

这让我怀疑途中是否存在某种数据损坏。或者您受到硬件故障的影响(最近我们在交换机和整个数据中心方面遇到了一些问题;)。在任何情况下,如果您对 uniprot.org 有任何疑问或问题,请联系 [email protected],然后我们可以看看我们是否可以帮助/解决问题。

Your code snippet seems normal and should work.

你的代码片段看起来很正常,应该可以工作。

Regards, Jerven Bolleman

问候, 杰文·博尔曼

回答by Legna

from the API documentation for the URLclass:

来自URL类的 API 文档:

The URL class does not itself encode or decode any URL components [...]. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL.

URL 类本身不编码或解码任何 URL 组件 [...]。调用者有责任对需要在调用 URL 之前转义的任何字段进行编码,并解码从 URL 返回的任何转义字段。

so if you have any spaces in your url-str encode it before calling new URL(url-str)

因此,如果您的 url-str 中有任何空格,请在调用新 URL(url-str) 之前对其进行编码

回答by shreyas

@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.

@posdef 我遇到了相同的 HTTP 错误代码 505 问题。当我在 Firefox 中粘贴我在 Java 代码中使用的 URL 时,Chrome 工作正常。但是通过代码给出了IOException。但最后我开始知道在 url 字符串中有方括号“(”和“)”,通过删除它们它可以工作,所以看起来我需要像浏览器一样的 URLEncodeing。