2011-02-17 3 views
0

나는 자바로 페이스 북에 로그인을 쓰려고합니다. 이것은 나의 코드이며, 나의 실수는 이해할 수 없다. 당신은 사용자가 사이트를 통해 페이스 북에 로그인하려면로그인 페이스 북에있는 자바

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

그리고 무엇이 문제입니까? 일부 오류 메시지가 표시됩니까? –

답변

1

, 당신은 자신이지고 당신이 계획하는 경우 (암호를 직접 전달 등 서버에 다시 토큰을 전달 포함 페이스 북의 API를 사용할 필요가)는 서비스 약관에 대해 가장 가능성이 큽니다.

HTTP 클라이언트를 사용하여 페이스 북에 대한 로그인을 자동화하려는 경우 다른 이야기 일 수 있습니다.

+0

페이스 북에 대한 내 로그인을 HTTP 클라이언트와 함께 자동화하려고합니다. – user621459

+0

HTTP 클라이언트와 함께 페이스 북에 로그인하는 것을 자동화하고 싶습니다. – user621459

+0

문제점을 발견했습니다. SSLSocketFactory.getSocketFactory() insted of SSLSocketFactory.getDefault() . 하지만 여전히 로그인 페이지를 가져올 수 없습니다. – user621459

관련 문제