1

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.

7
  • Maybe the PC there is sitting behind a proxy? Commented Aug 8, 2019 at 6:28
  • Corporate PC has proxy but it doesn't require any authentication. Commented Aug 8, 2019 at 6:31
  • Some firewall rules maybe? Commented Aug 8, 2019 at 6:32
  • I think u still need to tell Java that a proxy exists, i had a similar problem Commented Aug 8, 2019 at 6:33
  • Try: System.setProperty("http.proxyHost", "host"); System.setProperty("http.proxyPort", "port"); Commented Aug 8, 2019 at 6:33

1 Answer 1

1

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();
    }
Sign up to request clarification or add additional context in comments.

6 Comments

And u really have the right host and the right port? U need to replace the host with your host and the port with your port.
Yeah. I am using the same proxy in my browser as well as intellij
In what line do you get the Error?
This is where httpURLConnection.getResponseCode() I am getting error. In the debug mode I noticed inputstream as null
try to add this after the u.openConnection(): huc.setRequestMethod("GET"); huc.connect();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.