Https Verbindung aufbauen zum File Download

sebi13

Mitglied
Hey, ich wollte wohl eine Https Verbindung aufbauen, um eine Datei downzuloaden, jedoch komme ich nicht auf die Lösung, damit das auch richtig funktioniert.

Bekomme immer diese Exception:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


Hier mein Code:

Java:
package process_1;

import java.net.*;
import java.io.*;
import java.security.cert.*;
	
public class process_1 {

	public static void main(String[] args) {
		
		String username = "username";
		String password = "password";
		
		
		
		
		try {
			
			URL url = new URL("https-Adresse");
			String auth = username +  ":" + password;
			String encoding = new sun.misc.BASE64Encoder().encode(auth.getBytes());
			URLConnection uc = url.openConnection();
			uc.setRequestProperty("Authorization", "Basic " + encoding);
		
		
			InputStream data = (InputStream) uc.getInputStream();
			BufferedReader in = new BufferedReader (new InputStreamReader (data));
			
			String line;
			
			while((line = in.readLine()) != null) {
				System.out.println(line);
			}
		}
		

		
	
		catch (IOException e) {
			System.out.println("Konnte die Datei nicht lesen " + e.getMessage());	
		}
	
		
	}

}
 
Hier mal mein Code, der zumindest mit den Java-Forum Bild funktioniert:
Java:
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class DateiDownload {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://www.java-forum.org/images/misc/java_forum_org.gif");
		InputStream is = url.openStream();
		String filename = new File(url.getFile()).getName();
		File file = new File(System.getProperty("user.home")+"/Downloads/"+filename);
		file.createNewFile();
		FileOutputStream fos = new FileOutputStream(file);
		byte[] buffer = new byte[0xFFFF];
		for(int len; (len = is.read(buffer)) != -1;) {
			fos.write(buffer, 0, len);
		}
		fos.close(); 
	}
}
 
ne auf die URL komme ich per Browser ohne Probleme drauf, aber nicht aus meinem Java Programm, da der irgendwie Probs mit dem SSL Handshake hat, wieso auch immer
bei dir findet ja auch keine Authorisierung wie bei mir statt
 
So das Problem ist gelöst, für alle die was ähnliches machen wollen, habe gerade einen hilfreichen Link gefunden:

Fix certificate problem in HTTPS - Real's Java How-to

mein Quellcode sind nun folgendermaßen aus


Java:
package process_1;

import java.net.*;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.*;

	
public class process_1 {

	public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException{
		
		
		TrustManager[] trustAllCerts = new TrustManager[] {
			       new X509TrustManager() {
			          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			            return null;
			          }

			          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }

			          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }

			       }
			    };
		
		SSLContext sc = SSLContext.getInstance("SSL");
	    sc.init(null, trustAllCerts, new java.security.SecureRandom());
	    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

	    // Create all-trusting host name verifier
	    HostnameVerifier allHostsValid = new HostnameVerifier() {
	        public boolean verify(String hostname, SSLSession session) {
	          return true;
	        }
	    };
		
		HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
		
		String username = "username";
		String password = "password";
		

		try {
			URL url = new URL("https-Adresse");
			String auth = username +  ":" + password;
			String encoding = new sun.misc.BASE64Encoder().encode(auth.getBytes());
			URLConnection uc = url.openConnection();
			uc.setRequestProperty("Authorization", "Basic " + encoding);
		
		
			InputStream data = (InputStream) uc.getInputStream();
			BufferedReader in = new BufferedReader (new InputStreamReader (data));
			
			String line;
			
			while((line = in.readLine()) != null) {
				System.out.println(line);
			}
		}
	
	    catch (IOException e) {
	        System.out.println("Konnte die Datei nicht lesen " + e.getMessage());   
	    }
			
		
	}

}
 

Zurück
Oben