Search code examples
javajsoup

Using jsoup getting java.net.SocketTimeoutException: Read timed out exception


Using jsoup getting java.net.SocketTimeoutException: Read timed out exception

private static void getNiftyFutureOIReader() {
        String url = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=NIFTY&instrument=FUTIDX&type=-&strike=-&expiry=30JAN2020";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(15*1000).get();
            Element content = doc.getElementById("responseDiv");
            String jsonCont=content.html();
            System.out.println(jsonCont);

                } catch (IOException e) {
            e.printStackTrace();

        }

    }

i am using Jsoup to call website url and read its content, Using jsoup getting java.net.SocketTimeoutException: Read timed out exception

error log

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.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.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:750)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:722)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:306)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:295)
    at code.test.BankNiftyFutureOIReader.getNiftyFutureOIReader(BankNiftyFutureOIReader.java:19)
    at code.test.BankNiftyFutureOIReader.main(BankNiftyFutureOIReader.java:53)


Solution

  • Problem might be due to

    1. Make sure you are connected to the internet. Try to open same URL in the browser and see if it opens the page. or from Your VM able to reach that url simple curl / wget methods

    2. Specify more Jsoup connection time out before getting the document as given below.

    Ref: https://www.javacodeexamples.com/jsoup-sockettimeoutexception-read-timed-out-connect-timed-out-fix/775