java.io.IOException: 无效的 Http 响应

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

java.io.IOException: Invalid Http response

javahttpresponsehttpurlconnectionioexception

提问by AlexP

Now before you say there are questions like this I'd like to point out I've looked over most of them without any luck. Also I'm a first timer here so be gentle.

现在,在你说有这样的问题之前,我想指出我已经查看了其中的大部分,但没有任何运气。另外,我是第一次来这里,所以要温柔。

I have this annoyance right now in my current program:

我现在在我当前的程序中有这个烦恼:

Basically this part of my program uses a search engine to find torrent files.

基本上我程序的这部分使用搜索引擎来查找 torrent 文件。

public static ArrayList<String> search(String args) throws IOException {        
    args = args.replace(":", "");

    ArrayList<String> list = new ArrayList<String>();
    URL url = new URL("http://pirateproxy.net/search/" + args + "/");
    URLConnection con = url.openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); <---- THIS
}

public static void main(String[] args) {
    try {
        search("The Hobbit: The Desolation of Smaug");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

THE ERROR:

错误:

java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at service.ServiceDownloader.search(ServiceDownloader.java:20)
at service.ServiceDownloader.main(ServiceDownloader.java:45)

Now the fun part is that it ONLY goes wrong for this movie ("The Hobbit: The Desolation of Smaug"), every other movie works perfectly. I don't understand this. Please do help. (Also I have removed every unnecessary code from the search method)

现在有趣的是,它只在这部电影中出错(“霍比特人:史矛革的荒凉”),其他每一部电影都完美无缺。我不明白这个。请帮忙。(我还从搜索方法中删除了所有不必要的代码)

If I did not put enough information here please ask me for more.

如果我没有在这里提供足够的信息,请向我索取更多信息。

采纳答案by Abimaran Kugathasan

You should URL encode the String The Hobbit: The Desolation of Smaug, since you have special character there. Ex : space.

您应该对 String 进行 URL 编码The Hobbit: The Desolation of Smaug,因为那里有特殊字符。例如:空间。

回答by John Stoner

I suspect it tripped on the colon (:) not the space. Are there other titles with a colon?

我怀疑它在冒号 (:) 而不是空格上绊倒了。还有其他带冒号的标题吗?

回答by svarog

Instead of concatenating strings and needlessly creating interim strings and failing because the url is not encoded, you can use the built in UriBuilderto generate a valid URL

您可以使用内置的UriBuilder来生成有效的 URL,而不是连接字符串和不必要地创建临时字符串并因为 url 未编码而失败

URL path = UriBuilder.fromPath("http://pirateproxy.net")
            .path("search")
            .path("some movie ")
            .build()
            .toURL();

// http://pirateproxy.net/search/some%20movie%20