java.lang.IllegalArgumentException: protocol = http host = null

2022-09-04 05:24:07

对于这个链接 http://bits.blogs.nytimes.com/2014/09/02/uber-banned-across-germany-by-frankfurt-court/?partner=rss&emc=rss 这段代码不起作用,但是如果我为example放另一个:https://www.google.com 一切都很好:

    URL url = new URL("http://bits.blogs.nytimes.com/2014/09/02/uber-banned-across-germany-by-frankfurt-court/?partner=rss&emc=rss");
        URLConnection uc;
    uc = url.openConnection();
    uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16");
    uc.addRequestProperty("referer", "http://www.facebook.com");
    uc.connect();     

    this.input = uc.getInputStream();

我得到这个例外:

java.lang.IllegalArgumentException: protocol = http host = null
    at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:170)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:926)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
    at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2398)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1557)
    at UrlParser.<init>(UrlParser.java:48)
    at TikaParser.test_url_parser(TikaParser.java:186)
    at TikaParser.run(TikaParser.java:256)
    at java.lang.Thread.run(Thread.java:745)

我的代码有什么问题?


答案 1

当URL以而不是开头时,我遇到了同样的异常。例如:http:/http://http:/www.example.com

这是在方法中,所以它的上下文并不完全相同,但可能是一个类似的问题。org.springframework.web.client.RestTemplate.exchange()


答案 2

大多数情况下,此错误是由于不正确的URL而发生的。确保您点击的网址正确无误。正如 wsams 所提到的,尝试检查 url 中的 // 是否正确。http://localhost:8080 正确的模式