0

I have the following code, but when I run it I get an exception

"SocketTimeoutException" at openStream.

Code:

String urlStr = "https://www.nse-india.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=SCHNEIDER&series=EQ&fromDate=01-01-2020&toDate=29-02-2020&datePeriod=&hiddDwnld=true";

URL urlConn = new URL(urlStr);
InputStream in = urlConn.openStream();

When I execute the same URL from browser, it works fine.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chari
  • 11
  • 4
  • Do you probably use a proxy with the browser? – Henry Feb 29 '20 at 15:26
  • I believe this SO question is relevant. [Java read CSV file from the web](https://stackoverflow.com/questions/44292230/java-read-csv-file-from-the-web). When I tried your URL in my browser, it turned out to be a CSV file. See this [screen capture](https://imgur.com/a/cZvIXLq). As you can see, the URL in the image is different to the one in your code, so it does look like a redirect is occurring. – Abra Feb 29 '20 at 15:55
  • It would help to see the exception message. It feels like a connection issue anyway. – s.fuhrm Mar 01 '20 at 09:29

2 Answers2

0

When I execute the same URL from browser, it works fine.

There is obviously a difference in what your browser does and what your JVM does. I guess that your browser has a HTTP proxy server configured, but your application hasn't?

halfer
  • 19,824
  • 17
  • 99
  • 186
s.fuhrm
  • 438
  • 4
  • 9
0

The server looks for two request headers, the below code works

        String urlStr = "https://www.nse-india.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=SCHNEIDER&series=EQ&fromDate=01-01-2020&toDate=29-02-2020&datePeriod=&hiddDwnld=true";

        URL url = new URL(urlStr);
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("accept-language", "en-US,en;q=0.9");
        conn.setRequestProperty("user-agent", "MyJavaApp");
        InputStream in = conn.getInputStream();
edwgiz
  • 747
  • 5
  • 15
  • _The server looks for two request headers_ How do you know this? – Abra Feb 29 '20 at 15:58
  • From the browser, which handled the request without the timeout – edwgiz Feb 29 '20 at 15:59
  • And where is it displayed in the browser? (Pardon my ignorance.) – Abra Feb 29 '20 at 16:00
  • For example i'm using chrome, there's network panel in devtools https://developers.google.com/web/tools/chrome-devtools#network – edwgiz Feb 29 '20 at 16:03
  • SocketTimeoutException means that the server could not be reached, not that the server is not understanding. – s.fuhrm Feb 29 '20 at 16:15
  • Thank you. Firefox has [Browser Console](https://imgur.com/a/FU3H7G7) where I can see HTTP return code is 301 (permanently moved) but I can also see the request headers. – Abra Feb 29 '20 at 16:15
  • @Arba, yeah, it's redirecting from www.nse-india.com to www1.nseindia.com, but only if the request headers are present – edwgiz Feb 29 '20 at 16:21