1

I have below class to connect to an HTTPS url,

public class HKReader {

public static void main(String[] paramArrayOfString) {
    String str1 = paramArrayOfString[0];
    String str2 = paramArrayOfString[1];
    String str3 = paramArrayOfString[2];
    String str4 = paramArrayOfString[3];

    BufferedInputStream localBufferedInputStream = null;
    FileOutputStream localFileOutputStream = null;
    try {
        URL localURL = new URL(str1);
        URLConnection localURLConnection = localURL.openConnection();
        localURLConnection.setRequestProperty("Proxy-Authorization", "Basic" + new BASE64Encoder()
                .encode(new StringBuilder().append(str3).append(":").append(str4).toString().getBytes()));

        localBufferedInputStream = new BufferedInputStream(localURLConnection.getInputStream());
        localFileOutputStream = new FileOutputStream(new File(str2));
        int i;
        while ((i = localBufferedInputStream.read()) != -1) {
            localFileOutputStream.write(i);
        }
        return;
    } catch (MalformedURLException localMalformedURLException) {
        localMalformedURLException.printStackTrace();
        System.exit(-1);
    } catch (IOException localIOException3) {
        localIOException3.printStackTrace();
        System.exit(-1);
    } finally {
        try {
            if (localBufferedInputStream != null) {
                localBufferedInputStream.close();
            }
            if (localFileOutputStream != null) {
                localFileOutputStream.close();
            }
        } catch (IOException localIOException5) {
            localIOException5.printStackTrace();
            System.exit(-1);
        }
    }
}
}

And I execute the class using the below command in my unix server

$JAVA_HOME/bin/java -cp $LIB_DIR -Djdk.http.auth.tunneling.disabledSchemes="" -Dhttps.proxyHost=primary-proxy -Dhttps.proxyPort=8081 HKReader $url $target_file $proxyUser $proxyPassword

I have tried googling several times but I am getting below error.

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at 
sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

Can someone please help me where I am missing.

Vinay Pandey
  • 441
  • 3
  • 10
  • 22
  • Did you find out what was happening? I'm seeing something very similar. – foxx1337 Apr 24 '19 at 20:34
  • Sorry Iam too late to reply your comment. However, there was a restriction in our intranet server which did not allowed us to establish the connection as the url was third party website. – Vinay Pandey Sep 10 '19 at 15:21
  • It's fine, I just manually wrote the connection code and it works. – foxx1337 Sep 10 '19 at 16:43

0 Answers0