7

Kind Attn Moderators: Before marking this query as duplicate, please note I have checked these questions...

...and was unable to fix the issue. Also I believe the context & error is different from those, thus seeking help here.

Context: Download a csv file from NSEIndia website to a local folder (Note: Am able to download CSV files from other random websites).

Issue: Returns an error - javax.net.ssl.SSLException: Connection reset (Full error pasted below)

Observation: I am perplexed, as I faced this same issue yesterday, but after a couple of retries, it worked. I made no changes to code or settings.

Question: Is there anything I can do from my end to ensure this error is not seen with this specific website ?

Code:

// Using FileUtils from -> import org.apache.commons.io.FileUtils;
try {
        FileUtils.copyURLToFile(new URL("https://www.nseindia.com/content/fo/fo_mktlots.csv"),new File("D:\\Download\\t1.csv"));
    } catch (IOException e) {
        e.printStackTrace();
    }

Error:

javax.net.ssl.SSLException: Connection reset
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:324)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:267)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:262)
    at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1652)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:1038)
    at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:245)
    at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:285)
    at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:344)
    at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:746)
    at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:689)
    at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:717)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1610)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
    at java.base/java.net.URL.openStream(URL.java:1162)
    at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1456)
    at main.Test.dummy3(Test.java:327)
    at main.Test.main(Test.java:59)
    Suppressed: java.net.SocketException: Connection reset by peer
        at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:421)
        at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:441)
        at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:825)
        at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1007)
        at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:82)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:355)
        ... 17 more
    Caused by: java.net.SocketException: Connection reset
        at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:324)
        at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:351)
        at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:802)
        at java.base/java.net.Socket$SocketInputStream.read(Socket.java:937)
        at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:450)
        at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
        at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1409)
        at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:1022)
        ... 13 more
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
iCoder
  • 1,406
  • 6
  • 16
  • 35
  • You are probably being rate-limited. – Stephen C Dec 14 '19 at 05:43
  • 1
    this website accepts a limited set of cipher suites and only TLS 1.2. Try configuring your client to use a compatible version and set of ciphers. I won't check in the source code, but I bet on _SSLHandshakeException_ being raised but covered by _SSLException_. – Eugène Adell Dec 14 '19 at 08:11
  • 1
    TLS 1.2 example [link](https://stackoverflow.com/questions/38118397/setting-ssl-version-with-fileutils-copyurltofile) – vhoang Dec 14 '19 at 13:31
  • Update - Tried the suggestion of @vhoang but it dint work in this case. Also I notice the website allows the program to download file from afternoon till late evening only (manually can download any time). I suspect the website is under some redevelopment & hence this behavior. My knowledge of SSL is very basic & hence not sure if I can fix this at my end. Will continue to search to see if I can get this working. Thanks – iCoder Dec 19 '19 at 11:11

0 Answers0