Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache HttpClient to access facebook

Any examples, tips, guidance for the following scenario?

I have used Apache HttpClient to simulate the functionality of browser to access facebook through java application. to do that first i have to provide user credentials. i have used examples provided in the following web site.

http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/

But non of these methods works for facebook, following is the test code i have written for this purpose. i have not provided the all the methods written, only the method used to login to the facebook account is given here. relay appreciate any help

    private static int connectAndLogin(String email, String pass){
    logger.trace("Facebook: =========connectAndLogin begin===========");

    String httpResponseBody = getMethod("http://www.facebook.com/login.php");
    if(httpResponseBody == null){
        //Why don't we try again?
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            logger.trace(e.getMessage());
        }
        httpResponseBody = getMethod("http://www.facebook.com/login.php");
    }
    logger.trace("Facebook: ========= get login page ResponseBody begin===========");
    logger.trace(httpResponseBody);
    logger.trace("Facebook: +++++++++ get login page ResponseBody end+++++++++");

    logger.trace("Facebook: Initial cookies: ");
    List<Cookie> cookies = getCookies();
    if (cookies.isEmpty()) {
        logger.trace("Facebook: None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            logger.trace("Facebook: - " + cookies.get(i).toString());
        }
    }
    if(httpResponseBody == null){
        logger.warn("Facebook: Warning: Failed to get facebook login page.");
    }

    try
    {
        HttpPost httpost = new HttpPost("http://www.facebook.com/login.php");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("email", email));
        nvps.add(new BasicNameValuePair("pass", pass));
        //don't know if is this necessary
        nvps.add(new BasicNameValuePair("login", ""));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        logger.info("Facebook: @executing post method to:" + "http://www.facebook.com/login.php");

        HttpResponse loginPostResponse = getHttpClient().execute(httpost);
        HttpEntity entity = loginPostResponse.getEntity();

        logger.trace("Facebook: Login form post: " + loginPostResponse.getStatusLine());
        if (entity != null) {
            logger.trace("Facebook: "+EntityUtils.toString(entity));
            entity.consumeContent();
        } else {
            logger.error("Facebook: Error: login post's response entity is null");
            return FacebookErrorCode.kError_Login_GenericError;
        }

        logger.trace("Facebook: Post logon cookies:");
        cookies = getCookies();
        if (cookies.isEmpty()) {
            logger.trace("Facebook: None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                logger.trace("Facebook: - " + cookies.get(i).toString());
            }
        }

        int statusCode = loginPostResponse.getStatusLine().getStatusCode();

        logger.info("Facebook: Post Method done(" + statusCode + ")");



        switch(statusCode){
        case 100: break;//we should try again;
        case 301:
        case 302:
        case 303:
        case 307:
        {
            //redirect
    //                Header[] headers = loginPostResponse.getAllHeaders();
    //                for (int i=0; i<headers.length; i++) {
    //                    logger.trace("Facebook: "+headers[i]);
    //                }
     //                Header locationHeader =         loginPostResponse.getFirstHeader("location");
    //                if(locationHeader != null){
    //                    homePageUrl = locationHeader.getValue();
    //                    logger.info("Facebook: Redirect Location: " + homePageUrl);
    //                    if(homePageUrl == null 
    //                        || !homePageUrl.contains("facebook.com/home.php")){
    //                        logger.error("Facebook: Login error! Redirect Location Url not contains "facebook.com/home.php"");
    //                        return FacebookErrorCode.kError_Login_GenericError;
    //                    }
    //                } else {
    //                    logger.warn("Facebook: Warning: Got no redirect location.");
    //                }
        }
        break;
        default:;
        }
    }
    catch (IOException ioe)
    {
        logger.error("Facebook: IOException
" + ioe.getMessage());
        return FacebookErrorCode.kError_Global_ValidationError;
    }

    logger.trace("Facebook: =========connectAndLogin end==========");
    return FacebookErrorCode.Error_Global_NoError;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code, based on that sample, worked for me:

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://www.facebook.com/login.php");

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
    entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}

HttpPost httpost = new HttpPost("http://www.facebook.com/login.php");

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "******"));
nvps.add(new BasicNameValuePair("pass", "*******"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Double check we've got right page " + EntityUtils.toString(entity));

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
    entity.consumeContent();
}

System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}

httpclient.getConnectionManager().shutdown();  

I am not sure if your code was managing properly cookies (and session id kept within one of them), maybe that was the problem. Hope this will help you.

Just to make clear version issue: I was using HttpClient version 4.X, not the old one (3.X). They differ significantly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...