Cannot download file over https behind firewall ?

I have a program to download files :

publicclass TestServlet

{

publicstaticvoid main(String args[]){

Authenticator.setDefault(new AuthImpl());

if (args.length!=2){

System.out.println("Proper Usage: java -Dhttp.proxyHost=172.21.32.166 -Dhttp.proxyPort=80 TestServlet RemoteFileURL LocalFileName");

System.out.println("Usage Example:java -Dhttp.proxyHost=199.67.138.83 -Dhttp.proxyPort=8080 TestServlet https://url.com/csv/file.zip file.zip");

System.exit(0);

}

DataOutputStream out=null;

FileOutputStream fOut=null;

try

{

trustAllHttpsCertificates();

String urlStr = args[0];

HostnameVerifier hv =new HostnameVerifier(){

publicboolean verify(String urlHostName, SSLSession session){

System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());

returntrue;

}

};

HttpsURLConnection.setDefaultHostnameVerifier(hv);

System.out.println("\nConnecting to Website . . . "+urlStr);

URL url =new URL(urlStr ==null ?"https://url/Downloads/WC.csv" : urlStr);

System.out.println("\nConnecting . . . . ");

BufferedReader in =new BufferedReader(new InputStreamReader(url.openStream()));

System.out.println(". . . Connected");

System.out.print("\nDownloading the file . . . ");

int buff;

fOut=new FileOutputStream(args[1]);

System.out.print(" . .");

out=new DataOutputStream(fOut);

System.out.print(" . .");

while ((buff = in.read()) != -1){

fOut.write(buff);

}

in.close();

System.out.println(" . . . Done \n");

}

catch (Exception e){

e.printStackTrace();

}

finally{

try{

fOut.flush();

fOut.close();

System.exit(0);

}

catch(Exception e){

e.printStackTrace();

}

}

}

HostnameVerifier hv =new HostnameVerifier()

{

publicboolean verify(String urlHostName, SSLSession session){

//System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());

returntrue;

}

};

privatestaticvoid trustAllHttpsCertificates()throws Exception

{

// Create a trust manager that does not validate certificate chains:

javax.net.ssl.TrustManager[] trustAllCerts =new javax.net.ssl.TrustManager[1];

javax.net.ssl.TrustManager tm =new miTM();

trustAllCerts[0] = tm;

javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts,null);

javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

}

publicstaticclass miTMimplements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager{

public java.security.cert.X509Certificate[] getAcceptedIssuers(){

returnnull;

}

publicboolean isServerTrusted(java.security.cert.X509Certificate[] certs){

returntrue;

}

publicboolean isClientTrusted(java.security.cert.X509Certificate[] certs){

returntrue;

}

publicvoid checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)

throws java.security.cert.CertificateException{

return;

}

publicvoid checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)

throws java.security.cert.CertificateException{

return;

}

}

publicstaticclass AuthImplextends Authenticator{

protected PasswordAuthentication getPasswordAuthentication(){

String username =new String("guest");

String password =new String ("guest");

returnnew PasswordAuthentication(username, password.toCharArray());

}

}

}

This program works fine for downloading files, over both http and https from my home. When I run this on my desktop in the office it can download files over http, but cannot download https files.

When I try to download files over https, I get the foll. exception:

java.net.UnknownHostException: www.site.com

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:153)

at java.net.Socket.connect(Socket.java:452)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(DashoA12275)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(DashoA12275)

at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(DashoA12275)

at sun.net.www.protocol.https.HttpsClient.doConnect(DashoA12275)

There are 2 differences here, one being that my office desktop sits behind a firewall and there is a proxy in between.

In the office I run the program as :

java -Dhttp.proxyHost -Dhttp.proxyPort TestServlet [URL] [filename]

eg.:

java -Dhttp.proxyHost=235.67.138.84 -Dhttp.proxyPort=8080 TestServlet https://site.com/portal/Downloads/file.csv file.csv

This url requires authentication, the userid and pwd are hardcoded in the code.

Now, my problem is I cannot understand why this error would come. Is it because of the firewall or because of the proxy ?

Why is it that I can download over http successfully from my office desktop? If the problem is with the firewall or proxy, then even http protocol urls should give the same problem.

Please help.

Vinay

Message was edited by:

vinay_dsouza

Message was edited by:

vinay_dsouza

[9414 byte] By [vinay_dsouzaa] at [2007-11-26 16:53:32]
# 1
If you are using HTTPS you should be settinghttps.proxyHosthttps.proxyPortnot the http.* ones.
ejpa at 2007-7-8 23:21:13 > top of Java-index,Core,Core APIs...
# 2
Thanks ejp, your suggestion worked. That was easy, wasn't it.Never thought it would be this simple.Thanks a lot!
vinay_dsouzaa at 2007-7-8 23:21:13 > top of Java-index,Core,Core APIs...