私はJavaでログインFacebookにログインしようとしています。これは私のコードです、私の間違いは分かりません。あなたがこれを得ることにあなた自身を計画している場合などログインFacebookでJava
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();
}
}
問題は何ですか?いくつかのエラーメッセージ? –