我正在尝试编写一个警报系统来定期刮擦投诉站点,以查找有关我的产品的任何投诉。我正在使用Jsoup。下面是给我错误的代码片段。刮一个网站
doc = Jsoup.connect(finalUrl).timeout(10 * 1000).get();
这给了我错误
java.net.SocketException: Unexpected end of file from server
当我复制粘贴相同finalUrl字符串在浏览器中,它的工作原理。然后,我尝试简单的URL连接
BufferedReader br = null;
try {
URL a = new URL(finalUrl);
URLConnection conn = a.openConnection();
// open the stream and put it into BufferedReader
br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
doc = Jsoup.parse(br.toString());
} catch (IOException e) {
e.printStackTrace();
}
但事实证明,连接本身返回null(br为空)。现在的问题是,为什么粘贴在浏览器中的同一个字符串打开网站时没有任何错误?
完整堆栈跟踪是如下:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:774)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:771)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
at ComplaintsBoardScraper.main(ComplaintsBoardScraper.java:46)
您的网址是否以'http://'开头,您的服务器是否允许连接到端口'80'? – ollo 2013-03-11 09:45:17
@ollo yes网址以http://开头。服务器是一个不在我控制范围内的远程服务器。虽然当我在服务器上尝试“nc”命令时,它说:连接到complaintsboard.com 80端口[tcp/http]成功! – rishi 2013-03-11 14:02:21
你在URL中有没有非转义字符,或者是你的应用程序阻止了internetaccess?网址是否重定向到另一个? – ollo 2013-03-11 14:14:17