2011-02-17 66 views
0

我正在尝试写入用Java登录Facebook。这是我的代码,我不明白是我的错误。用Java登录Facebook

package org.testLogin.com; 

import java.io.IOException; 

import javax.net.ssl.SSLSocketFactory; 

import org.apache.commons.httpclient.Cookie; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpException; 
//import org.apache.commons.httpclient.HttpVersion; 
import org.apache.commons.httpclient.NameValuePair; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.methods.PostMethod; 
import org.apache.http.conn.ClientConnectionManager; 
import org.apache.http.conn.scheme.PlainSocketFactory; 
import org.apache.http.conn.scheme.Scheme; 
import org.apache.http.conn.scheme.SchemeRegistry; 
import org.apache.http.conn.scheme.SocketFactory; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.params.HttpProtocolParams; 

public class FormLogin { 
    static final String LOGON_SITE = "www.facebook.com"; 
    static final int LOGON_PORT = 443; 

    public FormLogin() { 
     super(); 
    } 

    public static void main(String[] args) throws HttpException, IOException { 
     // Set target URL 
     String strURL = "http://www.facebook.com"; 
     System.out.println("Target URL: " + strURL); 
     // Get initial state object 
     HttpClient httpclient = new HttpClient(); 
     // ---------Help Code----------------------------------------------------------------------------------- 
     SSLUtilities.trustAllHttpsCertificates(); 
     HttpParams params = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(params, 
       org.apache.http.HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(params, "UTF-8"); 
     HttpProtocolParams.setUserAgent(params,"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"); 
     HttpProtocolParams.setUseExpectContinue(params, false); 
//  HttpProtocolParams.setConnectionTimeout(params, 1000); 
//  HttpProtocolParams.setSoTimeout(params, 10000); 
     SchemeRegistry supportedschemes = new SchemeRegistry(); 
     supportedschemes.register(new Scheme("http", PlainSocketFactory 
       .getSocketFactory(), 80)); 
     supportedschemes.register(new Scheme("https", 
       (SocketFactory) SSLSocketFactory.getDefault(), 443)); 
     // .getSocketFactory() 
     ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(
       params, supportedschemes); 
     DefaultHttpClient client = new DefaultHttpClient(connectionManager, 
       params); 

     // -------------------------------------------------------------------------------------------------------------- 

     GetMethod httpget = new GetMethod(strURL); 
     // Execute HTTP GET 
     int result = httpclient.executeMethod(httpget); 
     // Display status code 
     System.out.println("Response status code: " + result); 
     // Get all the cookies 
     Cookie[] cookies = httpclient.getState().getCookies(); 
     // Display the cookies 
     System.out.println("Present cookies: "); 
     for(int i = 0; i < cookies.length; i++) { 
      System.out.println(" - " + cookies[i].toExternalForm()); 
     } 
     // Release current connection to the connection pool 
     httpget.releaseConnection(); 
     // Cookie 
     PostMethod postMethod = new PostMethod(
       "https://login.facebook.com/login.php?login_attempt=1"); 
//  int resultPost= httpclient.executeMethod(postMethod); 
     Cookie[] cookies1= httpclient.getState().getCookies(); 
     String CookieLsd = null; 
     for(int j=0; j<cookies1.length;j++) 
     { 
      String Check= cookies1[j].toString(); 
      if(Check=="lsd") 
      { 
//    CookieLsd= Check; 
       System.out.println(Check); 
      } 
     } 
     NameValuePair[] postData = new NameValuePair[6]; 
     postData[0] = new NameValuePair("locale", "en_US"); 
     postData[1] = new NameValuePair("non_com_login", ""); 
     postData[2] = new NameValuePair("email", "********"); 
     postData[3] = new NameValuePair("pass", "********"); 
     postData[4] = new NameValuePair("lsd", CookieLsd); 
     postData[5]= new NameValuePair("charset_test", "**************"); 
     postMethod.setRequestBody(postData); 

     int responsePage = httpclient.executeMethod(postMethod); 
     // for(int i = 0; i < cookies.length; i++){ 
     // postMethod.setRequestHeader("Cookie:",cookies[i].toExternalForm()); 
     // } 
     try { 
      httpclient.executeMethod(postMethod); 
     } 
     // int statuscode = postMethod.getStatusCode(); 
     // System.out.println("STATUS CODE = " + statuscode); 
     catch (HttpException httpe) { 
      System.err.print("HttpException"); 
      System.err.println(httpe.getMessage()); 
      httpe.printStackTrace(); 
     } 

     // catch (IOException ioe) { 
     // System.err.print("IOException"); 
     // System.err.println(ioe.getMessage()); 
     // ioe.printStackTrace(); 
     // } 
     String responseBody = postMethod.getResponseBodyAsString(); 
//  String responseBody = postMethod.getEntity(). 
     System.out.println(responseBody); 
     postMethod.releaseConnection(); 
    } 
} 
+1

问题是什么?一些错误信息? – 2011-02-17 14:18:58

回答

1

如果希望用户通过您的网站登录到Facebook上,你需要使用Facebook的API,其中包括令牌传递回您的服务器等传递自己的密码(在得到这个你自己,如果你打算)最有可能违反服务条款。

如果您尝试使用HTTP客户端将自己的登录自动登录到Facebook,这可能是一个不同的故事。

+0

我试图自动化我自己的登录到facebook,与一个HTTP clien – user621459 2011-02-17 21:48:34