@红薯 你好,想跟你请教个问题:
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List;
import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils;
import android.util.Log;
public class Login_http { private String username; // 用户名 private String password;// 用户密码 private int state; private String result;// 返回的数据
private String HOST = "http://210.36.16.166";// 主机
private String Land_Url = "http://210.36.16.166/success.yws";// 登陆地址
private String method;
private String cookie_id;
private String cookie_id_end;
private HttpClient client;
private HttpContext httpcontext ;
public Login_http(String username, String password) {
super();
this.username = username;
this.password = password;
Login_In();
}
// 实现登陆
public void Login_In() {
httpcontext = new BasicHttpContext();
client = new DefaultHttpClient();
HttpResponse responsepost;
HttpResponse responseget;
HttpResponse userinforesponse;
// 存放用户名和密码
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("username", username));
formparams.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity urlentity;
try {
urlentity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost post = new HttpPost(Land_Url);
post.addHeader("Referer", "http://210.36.16.166/index.yws");
post.addHeader("Accept",
"text/javascript, text/html, application/xml, text/xml");
post.addHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
post.addHeader("Accept-Encoding", "gzip,deflate");
post.addHeader("Connection", "Keep-Alive");
post.addHeader("Cache-Control", "no-cache");
post.addHeader("Cookie", cookie_id);
post.addHeader("Host", "210.36.16.166");
post.addHeader("Location", "http://210.36.16.166/index.yws");
post.setEntity(urlentity);
responsepost = client.execute(post, httpcontext);
HttpGet get = new HttpGet("http://bbs.newgxu.cn/index.yws");
responseget = client.execute(get);
HttpEntity entity1 = responsepost.getEntity();
HttpEntity entity2 = responseget.getEntity();
// System.out.println(EntityUtils.toString(entity1)); result = EntityUtils.toString(entity2);// 返回结果 // Log.i("EntityUtils", result); state = responsepost.getStatusLine().getStatusCode();// 200是联网正常
HttpUriRequest successurl = (HttpUriRequest) httpcontext
.getAttribute(ExecutionContext.HTTP_REQUEST); System.out.println(successurl.getURI().toString()); HttpGet userinfo=new HttpGet("http://bbs.newgxu.cn/user/upgrade.yws"); userinforesponse=client.execute(userinfo); HttpEntity userentity=userinforesponse.getEntity(); System.out.println(EntityUtils.toString(userentity)); if (successurl.getURI().toString().equals("/success.yws")) { System.out.println("登陆成功!"); System.out .println("dcfvgwdfgdiufhduiofheifh" + result.length()); } else { System.out.println( "登陆失败"); }
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面的 post 可以 成功 登陆。账号 (a1008611)密码(a1008611) 但我要登陆 后 http://bbs.newgxu.cn/user/upgrade.yws 的信息。却被重 定向到 登陆 界面了(http://bbs.newgxu.cn/login.yws)? 在这也可以 登陆!! http://bbs.newgxu.cn/index.yws 要登录 才能 看见 的网页(http://bbs.newgxu.cn/user/upgrade.yws)否则 会被 重定向 想到 登陆 界面。 我用的是 android 4.1 jar包。是不是 这里的apache 的jar 包 cookie 不会自动 保存 啊? 求教 ,谢谢!!版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。