-1

When I run the following code:

try {
    URL url = new URL("https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HUDCO&series=N2");
    File f = new File("/Users/Vaibhav/Desktop/nseurltest.txt");
    FileUtils.copyURLToFile(url, f);
} catch (Exception e) {
    e.printStackTrace();
}

I get a java.net.SocketException: Operation timed out after about 30 seconds. Up to about a month ago, the same code was running without error. What could be the reason for this exception suddenly, and how can I fix it?

The objective of this code is to ultimately extract the latest market price of the HUDCO N2 bond from the .txt file the URL is copied into. If there is another simple way to extract the market price from the URL, I would love to hear.

Robert
  • 33,260
  • 14
  • 84
  • 130
Vebby
  • 67
  • 1
  • 5

1 Answers1

1

I guess the website you try to reach, blocks unknown connections. But you can overcome this problem with jsoup library. With the following code, I've managed to download the content of the link.

            Response response = Jsoup.connect(
                    "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HUDCO&series=N2")
                    .ignoreContentType(true)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36")
                    .referrer("http://www.google.com").timeout(30000).followRedirects(true).execute();
            Document doc = response.parse();

JSOUP dependecy :

        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

You can change useragent. I put chrome 70 version's useragent string There are many options in the following link. http://www.useragentstring.com/pages/useragentstring.php

Volkan Albayrak
  • 314
  • 1
  • 11
  • Thank you!! This resolves my problem. I just replaced my code with your first code snippet, imported org.jsoup.Connection.Response, org.jsoup.Jsoup, and org.jsoup.nodes.Document, and extracted text from the document with doc.body().text(). I'm confused about the second code snippet though, what should I do with that? – Vebby Jan 08 '20 at 19:35
  • Don't worry about it. I added it for maven dependecy. I'm using maven :) – Volkan Albayrak Jan 08 '20 at 19:37
  • Hi, a new problem is arising with the code in your answer: I'm now getting a "java.net.SocketTimeoutException: Read timed out". Why is this happening and is there a way to fix it? – Vebby May 29 '20 at 14:10
  • @Vebby are you able to open the url in browser? – Volkan Albayrak May 29 '20 at 14:22
  • Yes, I am, in both the browsers I use: Chrome 83.0.4103.61 and Firefox 76.0.1 on my Mac (Catalina 10.15.4). There is a newer version of the website and this is the equivalent URL: https://www.nseindia.com/get-quotes/bonds?symbol=HUDCO&series=N2&maturityDate=05-Mar-2027. However, plugging in this URL in the code gives the same error. – Vebby May 29 '20 at 15:16
  • Nevermind, I fixed the problem by changing the userAgent to "Chrome/83.0.4103.61". Feel silly for not having tried this earlier. Thanks anyway :) – Vebby Jun 01 '20 at 11:54