title: apache HttpClient to access facebook
body: <p>Any examples, tips, guidance for the following scenario?</p>
  
  <p>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.</p>
  
  <p><a href="http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/" rel="nofollow noreferrer">http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/</a></p>
  
  <p>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</p>
  
  <pre class="lang-java prettyprint-override"><code>    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&lt;Cookie&gt; cookies = getCookies();
      if (cookies.isEmpty()) {
          logger.trace("Facebook: None");
      } else {
          for (int i = 0; i &lt; 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 &lt;NameValuePair&gt; nvps = new ArrayList &lt;NameValuePair&gt;();
          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 &lt; 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&lt;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;
  }
  </code></pre>