Taurus的网志

业精于勤而荒于嬉 行成于思而毁于随

导航

android+httpclient打造cjol客户端

问题背景:媳妇要找工作,简历发在cjol上。这几点是她最关心的内容:1、刷新简历(网站的功能,能改变简历发布时间让简历排在前面)  2、面试通知 3、简历浏览数  但她大部分时间都是要在外边面试。所以这3项工作自然就落到我的头上,每半小时就一个电话过来监督,好烦人。于是,动手能力一向很强的本人决定从问题出发,给cjol做个客户端,解决她以上需求。

要点:1、模拟http请求 2、解析html

难度:2颗小星星

工具:

1)Fiddler2 用他来监控浏览器和服务器间的所有请求,找到关键操作 http://www.cnblogs.com/chenjiajin/archive/2011/12/15/2288786.html

2)httpclient  Apache 出品必属精品,提供HTTP 协议的客户端编程工具包。用来模拟http请求

3)android环境

找出关键步骤了,剩下的就是模拟了。接着录下刷新、打开面试通知页面等步骤。用httpclient模拟即可。

贴代码:

package com.taurus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CjolActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private static final String LOGON_SITE = "http://www.cjol.com";
    private static final String USERNAME = "********";//你的用户名
    private static final String PASSWORD = "********";//你的密码

    private static final int LOGON_PORT = 80;
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnCheck = (Button) findViewById(R.id.btnCheck);
        tv = (TextView) findViewById(R.id.textView);
        btnCheck.setOnClickListener(this);
        try {
            main();
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    public void main() throws HttpException, IOException{
        StringBuffer sb = new StringBuffer();
            HttpClient client = new HttpClient();
            client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
            // 设置代理对象 ip/代理名称,端口
//            client.getHostConfiguration().setProxy("localhost", 8888);
            
// 登录页面
            PostMethod post = new PostMethod(
                    "http://www.cjol.com/jobseekers/Login.aspx?SkinId=Default");
            NameValuePair v1 = new NameValuePair(
                    "__VIEWSTATE",
                    "/wEPDwUJNDc3ODI3MzM3D2QWAgIDD2QWBAILDw9kFgIeDGF1dG9jb21wbGV0ZQUDb2ZmZAINDw8WAh4EVGV4dAUY55m75b2V55So5oi35ZCN5LiN5a2Y5ZyoZGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCGJ0bkxvZ2lue9S1/FB8b3fFVXy7iDffObaT+Iw=");
            NameValuePair v2 = new NameValuePair("__EVENTVALIDATION",
                    "/wEWBAKztpHiCwKl1bKzCQK1qbSRCwKC3IeGDNbrqRqhGg9TthU1uDa3AeymTebG");
            NameValuePair v3 = new NameValuePair("txtUserName", USERNAME);
            NameValuePair v4 = new NameValuePair("txtPassword", PASSWORD);
            NameValuePair v5 = new NameValuePair("btnLogin.x", "35");
            NameValuePair v6 = new NameValuePair("btnLogin.y", "9");
            post.setRequestBody(new NameValuePair[] { v1, v2, v3, v4, v5, v6 });
            client.executeMethod(post);
            System.out
                    .println("******************************登录******************************");
            Cookie[] cookies = client.getState().getCookies();
            post.releaseConnection();
            int statuscode = post.getStatusCode();// 得到相应状态码
            
// 判断是否是请求转发 是的话请求转发的地址
            if (statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                sb.append("登录成功"+"\n");
            else
                sb.append("登录失败"+"\n");
            System.out
                    .println("******************************首页******************************");
            String newUrl = "http://www.cjol.com/jobseekers/default.aspx";
            String s = "";
            for (Cookie c : cookies) {
                s += c + "; ";
            }
            GetMethod get = new GetMethod(newUrl);
            get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
            get.setRequestHeader("Cookie", s);
            get.setRequestHeader("Accept-Language", "zh-cn");
            client.executeMethod(get);
            String responseString = get.getResponseBodyAsString();
            String regex = ">([0-9]+)</font></span><span[\\s]+id=\"labViewResumeCountTip";
            sb.append("你的简历已被浏览了:" + oneStr(regex, responseString)
                    + "次"+"\n");
            get.releaseConnection();
            System.out
                    .println("******************************刷新的内容******************************");
            String resumeUrl = "http://www.cjol.com/jobseekers/Resume/UpdateResume.aspx";
            get = new GetMethod(resumeUrl);
            get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
            get.setRequestHeader("Cookie", s);
            get.setRequestHeader("Accept-Language", "zh-cn");
            client.executeMethod(get);
            if (get.getResponseBodyAsString().indexOf("简历刷新成功") > 0)
                sb.append("简历刷新成功"+"\n");
            else
                sb.append("简历刷新失败"+"\n");
            get.releaseConnection();
            System.out
                    .println("******************************通知******************************");
            String noticeUrl = "http://cjol.com/jobseekers/JobOpportunity/InterviewNotificationList.aspx";
            get = new GetMethod(noticeUrl);
            get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
            get.setRequestHeader("Cookie", s);
            get.setRequestHeader("Accept-Language", "zh-cn");
            client.executeMethod(get);
            responseString = get.getResponseBodyAsString();
            regex = "面试通知\\(([0-9]+)条\\)";
            sb.append("你有:" + oneStr(regex, responseString) + "封面试通知"+"\n");
            regex = "target=\"_blank\">([^<]*)</a>";
            List<String> list = duoStr(regex, responseString);
            for (String string : list) {
                sb.append(string+"\n");
            }
            get.releaseConnection();
        tv.setText(sb);
    }
    public  String oneStr(String regex, String responseString) {
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(responseString);
        if (m.find()) {
            return m.group(1);
        }
        return "";
    }

    public  List<String> duoStr(String regex, String responseString) {
        List<String> list = new ArrayList<String>();
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(responseString);
        while (m.find()) {
            list.add(m.group(1));
        }
        return list;

    }

    @Override
    public void onClick(View v) {
        try {
            main();
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

搞完收工。媳妇一脸的崇拜,获赠香吻一枚。之后,接电话接到手软。嘻嘻~~两天后,搞定工作!

总结:以上只是简单的实现,就一个activity,但发挥想象力,利用httpclient模拟http请求能做的事情还是很多的, 比如自动刷票、投票、签到。。。浏览器上能做的事情,都可以实现。

相关资源下载:

 Cjol.rar

posted on 2011-12-15 16:38  IamTaurus  阅读(1629)  评论(5)    收藏  举报