开发者社区> 问答> 正文

有关 httpclient cookie 的问题!!:报错 

@红薯 你好,想跟你请教个问题:

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 不会自动 保存 啊? 求教 ,谢谢!!

展开
收起
kun坤 2020-06-04 21:06:09 743 0
1 条回答
写回答
取消 提交回答
  • responsepost = client.execute(post, httpcontext); post方式执行后应该记录下cookie,get方式执行前也应该像post那样添加cookie到Header信息里。 ######但 httpclient 不是自动处理 cookie 吗? 如果 自己 写 应该 如何 记录cookie?######

    List<Cookie> cookies = client.getCookieStore().getCookies();
    if (!cookies.isEmpty()) {
    for (Cookie cookie : cookies) {
    String cookie_domain = cookie.getDomain(); String cookie_name = cookie.getName()); String cookie_value = cookie.getValue()); String cookie_version = cookie.getVersion(); String cookie_path = cookie.getPath(); } }
    ######得到cookie的多个属性,如何使用,让两级网页使用同一cookie便别身份。本人有点菜。麻烦了!谢谢了。######得到cookie的多个属性,如何使用,让两级网页使用同一cookie便别身份。本人有点菜。麻烦了!谢谢了。######回复 @迷途d书童 : 有cookie ,刚刚试过了!######回复 @迷途d书童 : 对了 如果 他后台 不用cookie 验证那么也就 没办法 了吗?######回复 @迷途d书童 : 额 麻烦 了 谢谢 ,我看看 如何 办吧!!######这个需要你自己在后台做好生成和验证cookie的处理,比如生成cookie的时候带上userID,验证cookie的时候跟据你自己定的规则来验证。

    2020-06-08 11:19:26
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载