Java通过Cookie模拟登录微博

笔者从开始了解到写出这篇博客一共只用了几天的时间,所以本文更适合小白参考
本文以weibo.cn为例,但只需修改Request Header中的部分参数,也可用与模拟登录weibo.com

浏览器端操作

firefox下有款插件挺好用的,但笔者只安装了Chrome,故本文只介绍Chrome下的操作。

  1. 在Chrome中打开”weibo.cn”,按下F12,在”Network”中勾选”Preserve log”
    preserve-log

  2. 点击”登录”,输入自己的帐号密码并登录

  3. 登录之后查看刚才的”Network”,下面出现了许多文件,从中找到以自己首页地址命名或命名为”home”的文件并点击,找到”Request Headers”一栏
    network-home

浏览器端的操作基本就是这样,接下来我们开始写代码

代码部分

  1. 为了方便操作,笔者将部分操作封装成了实体类。首先是操作Cookie的实体:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.BasicCookieStore;

    public class CookieInfo {

    private HttpClient client;

    private HttpPost post;

    private HttpGet get;

    private BasicCookieStore cookieStore;

    public HttpClient getClient() {
    return client;
    }

    public void setClient(HttpClient client) {
    this.client = client;
    }

    public HttpPost getPost() {
    return post;
    }

    public void setPost(HttpPost post) {
    this.post = post;
    }

    public HttpGet getGet() {
    return get;
    }

    public void setGet(HttpGet get) {
    this.get = get;
    }

    public BasicCookieStore getCookieStore() {
    return cookieStore;
    }

    public void setCookieStore(BasicCookieStore cookieStore) {
    this.cookieStore = cookieStore;
    }
    }
  2. 接下来定义WeiboLogin类,在里面写入登录操作方法。首先需要实例化```eInfo:

    private CookieInfo cookieInfo = new CookieInfo();

  3. 接下来设置Cookie登录策略,不设置的话微博会拒绝登录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public WeiboLogin(){
    //Cookie策略,不设置会拒绝Cookie Rejected, 设置策略保存Cookie信息
    cookieInfo.setCookieStore(new BasicCookieStore());

    CookieSpecProvider myCookie = new CookieSpecProvider() {
    public CookieSpec create(HttpContext httpContext) {
    return new DefaultCookieSpec();
    }
    };

    Registry<CookieSpecProvider> rg = RegistryBuilder.<CookieSpecProvider>create().
    register("myCookie", myCookie).build();

    cookieInfo.setClient(HttpClients.custom().setDefaultCookieSpecRegistry(rg).build());
    cookieInfo.setGet(new HttpGet());
    cookieInfo.setPost(new HttpPost());
    }
  4. 再写一个方法,将浏览器中刚才看到的”Request Headers”中的参数填入保存,同时加入一个输出语句,用于检验是否成功登录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public void Login() throws ClientProtocolException, IOException, URISyntaxException{

    String LoginUrl = "https://weibo.cn/?luicode=20000174";

    //添加Request Header,用于模拟登陆
    cookieInfo.getGet().setURI(new URI(LoginUrl));
    cookieInfo.getGet().addHeader("Content-Type", "text/html; charset=utf-8");
    cookieInfo.getGet().addHeader("Host", "weibo.cn");
    cookieInfo.getGet().addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
    cookieInfo.getGet().addHeader("API-RemoteIP", "192.168.0.1"); //模拟登录IP
    cookieInfo.getGet().addHeader("X-Forwarded-For","192.168.0.1");
    cookieInfo.getGet().addHeader("CLIENT-IP", "192.168.0.1");
    cookieInfo.getGet().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
    cookieInfo.getGet().addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    cookieInfo.getGet().addHeader("Accept-Encoding", "gzip, deflate, br");
    cookieInfo.getGet().addHeader("Referer", " https://passport.weibo.cn/signin/login?entry=mweibo&r=https%3A%2F%2Fweibo.cn%2F%3Fluicode%3D20000174&backTitle=%CE%A2%B2%A9&vt=");
    cookieInfo.getGet().addHeader("Connection", "keep-alive");
    cookieInfo.getGet().addHeader("Cookie", "自己的Cookie信息");

    HttpResponse response = cookieInfo.getClient().execute(cookieInfo.getGet());
    HttpEntity entity = response.getEntity();

    //将获取到的网页信息打印出来,检验是否登录成功
    String context = EntityUtils.toString(entity);
    System.out.println("获取的微博内容"+context);

    }
  5. 最后新建Test类,写入Main主方法,调用Login()方法即可。如果代码没有问题,那么到这里就能看到自己的首页源代码了。