# HTTPS mit Apache HTTPClient



## christian234 (5. Mrz 2007)

Laut http://jakarta.apache.org/commons/httpclient/sslguide.html muss man nichts ändern um eine Seite mit https anstatt http aufzurufen. Bei der Seite https://ssl.google-analytics.com/urchin.js die ich in einem neuen Browserfenster anzeigen kann klappt es allerdings nicht mit folgendem Client. Ich bekomme nur in etwa das angezeigt:

ÌÈ$4j@a?÷áZª?KŠ}ß?œeKoéH"jèØ1mGÄ9!+Ð+6n0¸UÕ÷…»Ow{»ßAzé9¤ï!ý
aber ein Status 200/OK


```
package gethttp.httpClient;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.*;


public class Browse
{
	
	int counter;
	
    public Browse() {
        super();
    }

    public static void main(String[] args) throws Exception {
    	
    	System.getProperties().setProperty("httpclient.useragent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070216 Firefox/1.5.0.10");
    	
    	HttpClient client = new HttpClient();
        
    	client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        
        
    	
    	//genericGet(client, "http://www.click*optio*ns.com", true, true, true, "");

         // funtioniert weder bei Aufruf der Seite davor noch bei Angabe des Referer!!!
       	genericGet(client, "https://ssl.google-analytics.com/urchin.js", true, true, true, "http://www.cli*ckop*tions.com");

       	//genericGet(client, "http://www.clic*kopti*ons.com/web/home.do?lang=fr&country=FR&flash=yes", true, true, true, "http://www.cli*ckop*tions.com");
        
       	genericGet(client, "https://msp.f-secure.com/web-test/", true, true, true, "");

       	
    	
    }
    	
       
 
  /*
        NameValuePair vp1   = new NameValuePair("loc", "de_DE");
        NameValuePair vp2   = new NameValuePair("loginServlet", "/web/home.do");
        NameValuePair vp3   = new NameValuePair("servletParams", "&loc=de_DE");
       NameValuePair vp4   = new NameValuePair("userName", "XXX");
        NameValuePair vp5   = new NameValuePair("junkTextPassword", "XXX");
        NameValuePair vp6   = new NameValuePair("passwort", "XXX");
    
*/
    
    
    
	static void genericGet(HttpClient aClient, String aURL, boolean aStatus, boolean aCookies, boolean aPrint, String aReferer) {
		
		
// auch mit diesem Code geht es nicht		
//		aClient.getHostConfiguration().setHost("ssl.google-analytics.com", 443, "https");
//		aURL ="/urchin.js";
			
		
		
	   	GetMethod mGetMethod = new GetMethod(aURL);
	    
	   	mGetMethod.setRequestHeader( new Header( "connection", "Keep-Alive" ) );
	   	mGetMethod.setRequestHeader( new Header( "Keep-Alive", "300" ) );
	             
	   	mGetMethod.setRequestHeader( new Header( "Accept-Language", "de-DE,en;q=0.9,de;q=0.8" ) );
	   	mGetMethod.setRequestHeader( new Header( "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" ) );
	   	mGetMethod.setRequestHeader( new Header( "accept-encoding", "gzip,deflate" ) );
	   	mGetMethod.setRequestHeader( new Header( "Content-Type", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" ) );
	   	mGetMethod.setRequestHeader( new Header( "Referer", aReferer ) );
	  
	   	
	   	
	   	System.out.println("GET URL: "+aURL);
	   	
	   	try {
		   	aClient.executeMethod(mGetMethod);
			
		   	if (aStatus == true) {	
	            System.out.println("Status: " + mGetMethod.getStatusCode());
		   	}

		   	if (aCookies == true) {	
		   		Cookie[] mCookies = aClient.getState().getCookies();
		        System.out.println("Present cookies: ");
		        for (int i = 0; i < mCookies.length; i++) {
		            System.out.println(" - " + mCookies[i].toExternalForm());
		        }
		   	}
   	
		   	if (aPrint == true) {	
		        
		   		String mPage;
				    
		        BufferedReader br = new BufferedReader(new InputStreamReader(mGetMethod.getResponseBodyAsStream()));
	
		        StringBuilder sb = new StringBuilder();
			    String nextline=null;
			    String nl = "\n";
			    
			    while((nextline = br.readLine()) != null) {
			    	sb.append(nextline);
					sb.append(nl);
			    }
				
			    mPage = sb.toString();
			    System.out.println("BEGIN HTML");			    
			    System.out.println(new String(mPage));
			    System.out.println("END HTML");			    
			    br.close();
			}
   	
	  
		   	
		   	
		   	
		   	
    	} catch (HttpException e) {
	      System.err.println("Fatal protocol violation: " + e.getMessage());
	      e.printStackTrace();
	    } catch (IOException e) {
	      System.err.println("Fatal transport error: " + e.getMessage());
	      e.printStackTrace();
	    } finally {
	    	mGetMethod.releaseConnection();
	    }  
	       
	   
	   
	   
	}
	


	
	
	
	
	
	
	
	static void genericPost(HttpClient aClient, String aURL, String[] aParamKey, String[] aParamValue, boolean aStatus, boolean aCookies, boolean aPrint, String aReferer) {
		
		// super.counter++;
		
	   	PostMethod mPostMethod = new PostMethod(aURL);
	    
	   	mPostMethod.setRequestHeader( new Header( "connection", "Keep-Alive" ) );
	   	mPostMethod.setRequestHeader( new Header( "Keep-Alive", "300" ) );
	             
	   	mPostMethod.setRequestHeader( new Header( "Accept-Language", "de-DE,en;q=0.9,de;q=0.8" ) );
	   	mPostMethod.setRequestHeader( new Header( "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" ) );
	   	mPostMethod.setRequestHeader( new Header( "accept-encoding", "gzip,deflate" ) );
	   	mPostMethod.setRequestHeader( new Header( "Content-Type", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" ) );
	   	mPostMethod.setRequestHeader( new Header( "Referer", aReferer ) );
	  
	   	mPostMethod.setRequestHeader( new Header( "Content-type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE));
	    
	   	
	   	NameValuePair[] mNameValuePairs = new NameValuePair[aParamKey.length];
	   	for (int i = 0; i < mNameValuePairs.length; i++) {
	   		mNameValuePairs[i] = new NameValuePair(aParamKey[i], aParamValue[i]);
		}
	   
	   	//    authpost.setRequestBody( 
	       //      new NameValuePair[] {vp1, vp2, vp3, vp4, vp5, vp6});
	    
	    mPostMethod.addParameters(mNameValuePairs);
	          
	           
	           
  
	   	
	   	
	   	System.out.println("POST URL: "+aURL);
	   	
	   	try {
		   	aClient.executeMethod(mPostMethod);
			
		   	if (aStatus == true) {	
	            System.out.println("Status: " + mPostMethod.getStatusCode());
		        
	            String redirectLocation="";
		        Header locationHeader = mPostMethod.getResponseHeader("location");
		        if (locationHeader != null) {
		        	redirectLocation = locationHeader.getValue();
		        } else {
		               // The response is invalid and did not provide the new location for
		               // the resource.  Report an error or possibly handle the response
		               // like a 404 Not Found error.
		        }
		        System.out.println("Redirect: " + redirectLocation);

		   	}

		   	if (aCookies == true) {	
		   		Cookie[] mCookies = aClient.getState().getCookies();
		        System.out.println("Present cookies: ");
		        for (int i = 0; i < mCookies.length; i++) {
		            System.out.println(" - " + mCookies[i].toExternalForm());
		        }
		   	}
   	
		   	if (aPrint == true) {	
		        
		   		String mPage;
				    
		        BufferedReader br = new BufferedReader(new InputStreamReader(mPostMethod.getResponseBodyAsStream()));
	
		        StringBuilder sb = new StringBuilder();
			    String nextline=null;
			    String nl = "\n";
			    
			    while((nextline = br.readLine()) != null) {
			    	sb.append(nextline);
					sb.append(nl);
			    }
				
			    mPage = sb.toString();
			    System.out.println("BEGIN HTML");			    
			    System.out.println(new String(mPage));
			    System.out.println("END HTML");			    
			    br.close();
			}
   	
	  
		   	
		   	
		   	
		   	
    	} catch (HttpException e) {
	      System.err.println("Fatal protocol violation: " + e.getMessage());
	      e.printStackTrace();
	    } catch (IOException e) {
	      System.err.println("Fatal transport error: " + e.getMessage());
	      e.printStackTrace();
	    } finally {
	    	mPostMethod.releaseConnection();
	    }  
	       
	   
	   
	   
	}

	
    
    
}
```


Eine andere Verbindung zu https://msp.f-secure.com/web-test/ funktioniert und ich bekomme Klartext. Auf der Seite wo ich mich einloggen möchte werden über google-analytics Cookies gesetzt und eventuell brauche ich die, um mich einloggen zu können für die Session. Daher würde ich gerne dieses Skript auf https://ssl.google... aufrufen.


----------



## Christian234 (5. Mrz 2007)

Sorry, ich gebe einfach zu schnell auf und übersehe die Details:

accept-encoding", "gzip,deflate" sollte ich natürlich nicht verwenden, wenn ich dann nicht bereit bin es auch zu entpacken....

aber vllt können andere auch davon lernen wenn ich die Fehler hier sammle ;-)


----------

