简体   繁体   English

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

[英]java.io.IOException: Invalid Http response

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.如果我没有在这里提供足够的信息,请向我索取更多信息。

You should URL encode the String The Hobbit: The Desolation of Smaug , since you have special character there.您应该对 String The Hobbit: The Desolation of Smaug进行 URL 编码,因为那里有特殊字符。 Ex : space.例如:空间。

I suspect it tripped on the colon (:) not the space.我怀疑它在冒号 (:) 而不是空格上绊倒了。 Are there other titles with a colon?还有其他带冒号的标题吗?

Instead of concatenating strings and needlessly creating interim strings and failing because the url is not encoded, you can use the built in UriBuilder to 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM