Ok... in my quest for surfing the web programmatically in android. I am finally convinced that apache http api is the way to go. I need some help using it though. I have managed to login using httppost however I would like to be able to continue after that. taking the facebook example again, I login to facebook, now I want to use this login session to lets say post a status, how do I proceed? this is the code so far(requires cleaning and stuff, it was mostly copied from various sources)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("https://www.facebook.com/");
try{
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
// System.out.println("Login form get: " + response.getStatusLine());
// EntityUtils.consume(entity);
// 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());
}
}
// https://www.facebook.com/login.php?login_attempt=1
HttpPost httpost = new HttpPost("https://www.facebook.com/login.php?login_attempt=1");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "****"));
nvps.add(new BasicNameValuePair("pass", "****"));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entity = response.getEntity();
Toast.makeText(getBaseContext(), "done",Toast.LENGTH_LONG);
System.out.println("Login form get: " + response.getStatusLine());
// EntityUtils.consume(entity);
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());
}
}
}finally {} }catch (Exception e) {
// TODO: handle exception
}finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
Toast.makeText(getBaseContext(), "done",Toast.LENGTH_LONG);
}
}
}
Replace the stars with your email and password and this gets you inside facebook, how do I proceed from there to post a status?
again please don't tell me there is a facebook api, I don't want to use it, the point here is to learn to use apache libraries
The best way to start would be to wireshark and see what a browser is doing when posting a status. Then adapt to it in the HTTPClient.
在Mobile Client中保持会话的最佳方法应该是使用OAuth。当您获得OAuth令牌时,可以使用该令牌来使用Facebook提供程序提供的任何api。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.