无验证码登录获取cookie抓取数据

1引入maven

<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>
</dependency>

2.登录获取Cookie方法封装

//登录
private String login(HttpClient httpClient,String username,String password) {
    Cookie[] cookies = null;
    // 登陆 Url
    String loginUrl = "loginUrl";
    // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
    PostMethod postMethod = new PostMethod(loginUrl);
    // 设置登陆时要求的信息,用户名和密码
    NameValuePair[] data = {new NameValuePair("j_username",username), new NameValuePair("j_password", password)};
    postMethod.setRequestBody(data);
    try {
        int statusCode = httpClient.executeMethod(postMethod);
        // 获得登陆后的
        cookies = httpClient.getState().getCookies();
        //重定向到新的URL
        if (statusCode == 302) {
            System.out.println(username + "登录成功"); logger.error(username + "登录成功");
        } else {
            System.out.println(username + "登录失败"); logger.error(username + "登录成功");
        }
        if (cookies != null) {
            StringBuffer strCookies = new StringBuffer();
            for (Cookie c : cookies) {
                strCookies.append(c.toString() + ";");
                System.out.println("cookies = " + c.toString());
            } logger.error("cookies = " + strCookies.toString());
            return strCookies.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(username+ "登录异常, ex = " + e.getMessage());
    }
    return null;
}

3.根据Cookie获取数据方法封装

// 获取sim卡总数量
private int getTotalCount(HttpClient httpClient, String cookies) {
    String dataUrl = "xxxx";
    int count = 0;
    try {
        // 进行登陆后的操作
        GetMethod getMethod = new GetMethod(dataUrl);
        // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
        getMethod.setRequestHeader("cookie", cookies);
        httpClient.executeMethod(getMethod);
        if (getMethod.getStatusCode() == 200) {
            // 打印出返回数据,检验一下是否成功
            String text = getMethod.getResponseBodyAsString();
            JSONObject json = JSONObject.fromObject(text);
            count = (int) json.get("totalCount");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

 

4:获取数据

HttpClient httpClient = new HttpClient();
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略 
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
String cookies =login(httpClient,"xxxx","xxxx");
int count=getTotalCount(httpClient,cookies);

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值