[英]apache http client
Ok... in my quest for surfing the web programmatically in android. 好的...为了在Android中以编程方式浏览网络。 I am finally convinced that apache http api is the way to go.
我终于确信,Apache http api是必经之路。 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.
我已经设法使用httppost登录,但是之后我希望能够继续。 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?
再次以facebook为例,我登录到facebook,现在我想使用此登录会话来说发布状态,我该如何进行? 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? 用您的电子邮件和密码替换星号,这样您就可以进入Facebook,如何从那里发布状态?
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 再次请不要告诉我有一个Facebook API,我不想使用它,这里的重点是学习使用apache库
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.
然后在HTTPClient中适应它。
在Mobile Client中保持会话的最佳方法应该是使用OAuth。当您获得OAuth令牌时,可以使用该令牌来使用Facebook提供程序提供的任何api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.