代码是:
Document doc = Jsoup.connect("http://ws.audioscrobbler.com /2.0/?method=track.getInfo&api_key=550633c179112c8002bc6a0942d55b2a&artist=lucinda williams&track=lake charles")
.userAgent("Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0")
.timeout(5000)
.get();
但是,发生了一些错误:
Exception in thread "main" java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:767)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1162)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:397)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:429)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
at JsoupXML.main(JsoupXML.java:16)
但是,当我使用 brwoser 访问 url 时,一切都可以。此外,当我使用上面的代码来抓取http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=550633c179112c8002bc6a0942d55b2a&artist=cher&track=believe的内容时,一切都很好。
你能知道原因和解决它的好主意吗?
感谢您的关注,并对我的英语感到抱歉。
感谢 NeplatnyUdaj 的帮助,你给了我很好的提示。我忘了用 %20,%26 等替换空格和其他特殊符号。