Vikram
Vikram

Reputation: 7517

java.net.SocketException: Connection reset for inputstream

I am trying to download a file from https URL. I can able to download the file using the below code from my home PC. However, I am unable to download it from corporate machine. Required cacerts already imported in java properly. I can also able to download the file from the browser. I've looked at the number of other threads but not able to figure out the appropriate solution for this problem. Here is my Java code

 try
  {
    url = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
            inputStream = httpURLConnection.getInputStream();
        else
            inputStream = httpURLConnection.getErrorStream();
 }
 catch (Exception ioe)
 {
    ioe.printStackTrace();
 }

Here is the exception which I got. I am always getting exception at inputstream. Remember this code is working 100% on my home PC.

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)

Any clue will be a great help.

Upvotes: 1

Views: 2902

Answers (1)

ILikeSahne
ILikeSahne

Reputation: 170

Works fine for me:

    System.setProperty("http.proxyHost", "host");
    System.setProperty("http.proxyPort", "port");
    try {
        URL u = new URL("some url");
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        InputStream is = null;
        if (huc.getResponseCode() == HttpURLConnection.HTTP_OK)
            is = huc.getInputStream();
        else
            is = huc.getErrorStream();
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 1

Related Questions