2010-11-03 63 views
2

我想用java登錄到互聯網上的https站點,然後閱讀一些信息。我已經看過與螢火蟲的頭,但我不能設法使其...java https登錄和瀏覽

螢火告訴:

https://service.example.net/xxx/unternehmer/login.html?login=Anmelden&loginname=xxx&password=xxx&sessionid=&sprache=de 

然後,我要瀏覽這個網站:

https://service.example.net/xxx/unternehmer/ausgabe.html?code=PORTAL;sessionid=03112010150442 

我該如何用java做到這一點? 我已經嘗試過類似:

import java.net.*; 
import java.io.*; 
import java.security.*; 
import javax.net.ssl.*; 

public class HTTPSClient { 

    public static void main(String[] args) { 
    int port = 443; // default https port 
    String host = "service.example.net"; 
    try { 
     SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 

     SSLSocket socket = (SSLSocket) factory.createSocket(host, port); 

     // enable all the suites 
     String[] supported = socket.getSupportedCipherSuites(); 
     socket.setEnabledCipherSuites(supported); 


     Writer out = new OutputStreamWriter(socket.getOutputStream()); 
     // https requires the full URL in the GET line 
     out.write("POST https://" + host + "//xxx/unternehmer/login.html?login=Anmelden&loginname=xxx&password=xxx&sessionid=&sprache=de HTTP/1.1\r\n"); 
     out.write("Host: " + host + "\r\n"); 
     out.write("\r\n"); 
     out.flush(); 

     // read response 
     BufferedReader in = new BufferedReader(
     new InputStreamReader(socket.getInputStream())); 

     // read the header 
     String s; 
     while (!(s = in.readLine()).equals("")) { 
      System.out.println(s); 
     } 
     System.out.println(); 

     // read the length 
     String contentLength = in.readLine(); 
     int length = Integer.MAX_VALUE; 
     try { 
     length = Integer.parseInt(contentLength.trim(), 16); 
     } 
     catch (NumberFormatException ex) { 
     // This server doesn't send the content-length 
     // in the first line of the response body 
     } 
     System.out.println(contentLength); 

     int c; 
     int i = 0; 
     while ((c = in.read()) != -1 && i++ < length) { 
     System.out.write(c); 
     } 

     System.out.println("1.part done"); 

     out.close(); 
     in.close(); 
     socket.close(); 

    } 
    catch (IOException ex) { 
     System.err.println(ex); 
    } 

    } 

} 

不幸的是,這並不工作,爲登錄.... ,我也不知道從哪裏得到這個會話ID ......每次它是一個不同的。 我希望你能幫助我。 ps:我用xxx替換了一些相關信息

回答

4

問題解決了:)

首先我添加庫從阿帕奇:

  1. HttpClient的
  2. 公地httpclient的
  3. 公地編解碼器
  4. 共享記錄

然後我結合了幾個導師IALS。

我的代碼:

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import org.apache.commons.httpclient.Header; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.methods.PostMethod; 
import org.apache.http.client.params.CookiePolicy; 

    public class Test { 

    public static final String TARGET_HTTPS_SERVER = "www.example.net"; 
    public static final int TARGET_HTTPS_PORT = 443; 

    public static void main(String[] args) throws Exception { 

     HttpClient httpClient = new HttpClient(); 
     httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 

     PostMethod post = new PostMethod("https://www.example.com/login.html"); 
     post.setRequestHeader(new Header(
       "User-Agent", "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0")); 

     post.addParameter("login", "true"); 
     post.addParameter("username", "xxx"); 
     post.addParameter("password", "xxx"); 
     post.addParameter("language", "de"); 
     httpClient.executeMethod(post); 


     System.out.println(post.getResponseBodyAsString()); 
     String body=post.getResponseBodyAsString(); 
//Get the session id by parsing the code, i know this is not pretty 
      String sessionid=body.substring(body.indexOf("session")+10,body.indexOf("session")+10+14); 
      System.out.print(sessionid); 


      GetMethod get=new GetMethod("https://www.example.com/thesiteyouwannabrowse?sessionid="+sessionid); 

     get.setRequestHeader(new Header(
      "User-Agent", "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0")); 
     httpClient.executeMethod(get); 

     System.out.println(get.getResponseBodyAsString()); 
     //write it into a file 
     try{ 
       // Create file 
       FileWriter fstream = new FileWriter("file.html"); 
        BufferedWriter out = new BufferedWriter(fstream); 
       out.write(get.getResponseBodyAsString()); 
       //Close the output stream 
       out.close(); 
       }catch (Exception e){//Catch exception if any 
        System.err.println("Error: " + e.getMessage()); 
      }  
     post.releaseConnection(); 
    } 
    } 
3

我自己也做過類似的事情。我使用這種「手動」的方式得到了它的工作,但是這非常麻煩,尤其是在cookie管理方面。我建議你看看Apache HttpClient library。 (當我意識到使用這個庫是多麼容易的時候,我扔掉了代碼)。

正如org.life.java指出的,在這裏http://hc.apache.org/httpclient-3.x/sslguide.html是一個很好的howto如何開始使用這個庫的SSL 。

+0

http://hc.apache.org/httpclient-3.x/sslguide.html – 2010-11-03 13:58:47

+0

啊,很好的鏈接。更新了答案。 – aioobe 2010-11-03 14:01:40

+0

ty非常有幫助 – Umit 2010-11-09 08:22:13