I get a SocketTimeoutException
when I try to parse a HTML page using Jsoup:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at app.ForumCrawler.crawl(ForumCrawler.java:50)
at Main.main(Main.java:15)
I have used this part of code for parsing the page, since I look for the response like 200,404 etc..
String userAgent = "Mozilla/5.0 (jsoup)";
int timeout = 5 * 1000;
Document localDoc = null;
String url = "<url>";
Connection.Response response = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).execute();
if(response.statusCode() == 200) {
localDoc = Jsoup.parse(response.body());
//do the stuff..
}
I have come across that if we use .get()
instead of .execute()
we can get rid of SocketTimeoutException
issue, but if I use .get()
then I wasn't able to get response.
please suggest me which one to use to get rid of SocketTimeoutException
and also to get response
when it is trying to parse the page.
Thanks in advance.