gpt4 book ai didi

java - 用 Java 登录 Facebook

转载 作者:行者123 更新时间:2023-12-01 15:56:04 26 4
gpt4 key购买 nike

我正在尝试用 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();
}
}

最佳答案

如果您希望用户通过您的网站登录 Facebook,您需要使用 Facebook API,这涉及将 token 传递回您的服务器等。您自己传递密码(如果您打算自己获取密码)是最有可能的违反服务条款。

如果您尝试使用 HTTP 客户端自动登录 Facebook,情况可能会有所不同。

关于java - 用 Java 登录 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5030080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com