Accessing Facebook Through Java application

Category: , , , , , ,

This article will discuss following key points ,

Login facebook Using Apache HttpClient library and Facebook
RestAPI. Setting facebook status message using HttpClient
library and Facebook RestAPI and if possible we will discus
how to update fb profile data through java application as
well. you fist have to have following libraries with you,

commons-codec-1.4.jar
httpclient.jar
httpcore.jar
httpmime.jar

First HttpClient Instance which will be used through out the program.


--------------------------------------------------------------------------
HttpClient httpclient = new DefaultHttpClient();
--------------------------------------------------------------------------


now , if you just need to read a webpage , follow this step


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


and then using the above created instance, execute this GET method


--------------------------------------------------------------------------
HttpResponse response = httpclient.execute(httpget);
--------------------------------------------------------------------------


next problem is how do you read the output received performing
this get request,


--------------------------------------------------------------------------
HttpEntity entity = response.getEntity();
if (entity != null) {

String responseStr = EntityUtils.toString(entity);
// System.out.println(responseStr);
}
--------------------------------------------------------------------------


That is all you need to do in order to deal with HttpClient library for retrieving webpages only.Following is a summery of above steps for retrieving Facebook Homepage


--------------------------------------------------------------------------
/*Step1 : Create httpclient instance*/

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.facebook.com/login.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
String responseStr = EntityUtils.toString(entity);
// System.out.println(responseStr);
}


<--------------------------------------------------------------------->

Next thing to be discussed is , how to make an post using HttpClient library, Lets follow the following example of login to the fb.

Fist create HttpPost instance as you created that for HttpGet call. For the example we are following, posting is done on facebook site. its as follows


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


next step is to identify the parameters that you need to pass to perform this post request. we add all the parameters in to array list. array list contains objects of the type NameValuePair.


--------------------------------------------------------------------------
List nvps = new ArrayList();
--------------------------------------------------------------------------

next thing is to identify what are the parameters which should be passed with post request, you should have question how do I know these name value pairs to pass with the post request. this name value pairs depends on

nvps.add(new BasicNameValuePair("email", "harini.sync@gmail.com"));
nvps.add(new BasicNameValuePair("pass" , "pass1234"));
nvps.add(new BasicNameValuePair("login", ""));

Comments (0)

Post a Comment