gpt4 book ai didi

java - Apache http客户端

转载 作者:行者123 更新时间:2023-12-01 15:21:32 25 4
gpt4 key购买 nike

好吧...我正在寻求在 Android 中以编程方式上网。我终于确信 apache http api 是正确的选择。不过我需要一些使用它的帮助。我已成功使用 httppost 登录,但我希望之后能够继续。再次以 facebook 为例,我登录到 facebook,现在我想使用此登录 session 来发布状态,我该如何继续?这是到目前为止的代码(需要清理和其他东西,它大部分是从各种来源复制的)

  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);
}

}
}

用您的电子邮件和密码替换星星,这样您就可以进入 Facebook,我如何从那里继续发布状态?

再次请不要告诉我有 facebook api,我不想使用它,这里的重点是学习使用 apache 库

最佳答案

最好的开始方法是使用wireshark并查看浏览器在发布状态时正在做什么。然后在HTTPClient中进行适配。

关于java - Apache http客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10842843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com