Using Java to Download Files from an HTTPS URL

I wrote code to download a file from a website. The code works fine with a test http address. As soon as I changed the URL to https, I get the connection time.

System.setProperty("http.proxyHost","trproxy.rwe.com") ;
System.setProperty("http.proxyPort", "80") ;
Authenticator.setDefault (new MyAuthenticator("USER","PW"));
//URL url = new URL("http","www.treasury.gov",80,"/ofac/downloads/sdn.csv",new    sun.net.www.protocol.http.Handler());  THIS WORKS
URL url = new URL("https", "downloads.elexonportal.co.uk",443,"/bmradataarchive/download?key=MYKEY&filename="+filename,new sun.net.www.protocol.https.Handler());
url.openConnection();
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("C:/downloads/"+filename);

If I copy https-url to the browser, I will be asked where I want to save the file, and it works fine. Any help is greatly appreciated. I tried this one but didn't work

Thank you, Chris

+5
source share
3 answers

You may have a certificate problem. This is usually a problem that I encountered in the past when working with HTTPS connections in Java.

, URL, , ( ).

URL- .

FYI, , , , Java. Stackoverflow , Java: Java?

, , . , , ( ): https- java keystore

, , -. , , -.

HttpClient java.net.URL. Apache HttpClient 4.2.1.

, HTTP HTTPS, WebDAV.

Jakarta Slide WebDAV Client . , , , .

. , http, https.

HTTPS :

  • https.proxyHost
  • https.proxyPort

:

System.setProperty("https.proxyHost","trproxy.rwe.com") ; 
System.setProperty("https.proxyPort", "443") ; 

2.2 Oracle Java Networking Proxies.

http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

+4

, -. https. , HttpClient 4 , http-.

+1

System.setProperty("http.proxyPort", "80") it should be System.setProperty("http.proxyPort", "443")

0
source

All Articles