Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:43:09+00:00 2026-05-13T05:43:09+00:00

Any examples, tips, guidance for the following scenario? I have used Apache HttpClient to

  • 0

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\n" + ioe.getMessage());
        return FacebookErrorCode.kError_Global_ValidationError;
    }

    logger.trace("Facebook: =========connectAndLogin end==========");
    return FacebookErrorCode.Error_Global_NoError;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:43:10+00:00Added an answer on May 13, 2026 at 5:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any examples, tips, guidance for the following scenario? I have retrieved updates from several
The examples I have seen are geared towards C#. Any tips would be appreciated.
Does anyone have any examples or resources where i might find information on scrolling
Does anyone have any examples of using Sqlite with ASP.NET membership? I am building
does anyone have any examples of using NSKeyedArchiver in monotouch? i just want to
I have Googled a bit, and cannot seem to find any examples of Xaml-fying
I have been looking for ages and I can't find any good examples of
Are there any tips to speed up debugging in Codegear? For example, when I
I am looking for any examples or guides to using Linq over WCF (n-tier
Do you know of any examples of MooTools and ASP.NET MVC?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.