自动登录开心网

一 思路(需要的jar包:httpclient-4.2.jar, httpcore-4.2.jar,commons-codec-1.3.jar,commons-httpclient-3.1.jar 点击下载

1 创建httpclient 2 创建post方法用来登录(url,params)3 执行方法 4得到响应的有效信息(set-Cookie)5释放post方法

6 创建httpclient2 7创建get方法用来进入登录后网站里的页面来测试(header中加cookie) 8执行方法 4得到响应的html,进行索引,看是否含有用户名名字,检测登录情况

二 代码主要方法、名称

1 HttpClient 2 PostMethod 3 addParameter 4 executeMethod 5 getResponseHeader("set-Cookie") 6 releaseConnection 7 GetMethod

三 主要工具

firebug----用来获取登录url

四 主要代码

    public static void kaixinwang() {
        String loginUrl="https://security.kaixin001.com/login/login_auth.php";
        String url2="http://www.kaixin001.com/home";
        HttpClient httpClient=new HttpClient();
        PostMethod post=new PostMethod(loginUrl);
        post.addParameter("email","XXXX");//登录账号
        post.addParameter("password","XXXX");//登录密码
        try {
            httpClient.executeMethod(post);
            System.out.println(post.getResponseBody());
            String cookie="";
            Header[] headers=post.getResponseHeaders("set-Cookie");
            post.releaseConnection();
            for(int i=0;i<headers.length;i++){
                String str[]=headers[i].toString().trim().substring(12).split(";");
                for(int j=0;j<str.length;j++){
                    cookie=cookie+str[j]+";";
                }
            }
            HttpClient httpClient2=new HttpClient();
            GetMethod get=new GetMethod(url2);
            get.addRequestHeader("cookie", cookie);
            httpClient2.executeMethod(get);
            get.releaseConnection();
            boolean name=get.getResponseBodyAsString().contains("XXX");//您登录后应该显示的用户名称
            System.out.println(name);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 五 提高篇(代码重构)

上面代码的缺点:1 一个客户只应该新建1个httpclient 2 cookie的获取烦了 Cookie[] cookies=client.getStatus.getCookies(); 3 在此基础上,进行自动发表我的状态,给好友发私信

    public static void kaixinwang3() throws HttpException, IOException {
        String loginUrl = "https://security.kaixin001.com/login/login_auth.php";
        String url2 = "http://www.kaixin001.com/home";
        HttpClient httpClient = new HttpClient();
        PostMethod post = new PostMethod(loginUrl);
        post.addParameter("email", "18321867976");
        post.addParameter("password", "XXXX");
        httpClient.executeMethod(post);
        Cookie[] cookies = httpClient.getState().getCookies();
        GetMethod get = new GetMethod(url2);
        get.addRequestHeader("cookie", cookies.toString());
        httpClient.executeMethod(get);
        boolean isLogin = get.getResponseBodyAsString().contains("丁");//判断响应页面上是否有我的用户名字
        System.out.println(isLogin);//如果为true,说明登录成功了
        //////////////////////////////////发消息给好友
        PostMethod PostMethod1 = new PostMethod("http://www.kaixin001.com/msg/post.php");
        PostMethod1.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        NameValuePair uids    = new NameValuePair( "uids" , "155183021");//发送的好友对象的id qq796045356 
        NameValuePair content = new NameValuePair( "content" , "丁林11");//需要发送的信息的内容 155183021  155122535
        NameValuePair[] data1 = {uids,content};
        PostMethod1.setRequestBody(data1);
        httpClient.executeMethod(PostMethod1);
        System.out.println(PostMethod1.getStatusLine());
        //////////////////////////////发表自己的状态http://www.kaixin001.com/records/ajax/submit.php?customname=dfsdadfsaas&my_text=dfsdadfsaas&save_spri=0&save_to_album=1&spri=0&sync_status=1
        PostMethod post2=new PostMethod("http://www.kaixin001.com/records/ajax/submit.php");
        post2.addParameter("customname","dddddddddddddddddd");
        post2.addParameter("my_text","dddddddddddddddddd");
        post2.addParameter("save_to_album","1");
        post2.addParameter("sync_status","1");
        post2.addParameter("save_spri","0");
        post2.addParameter("spri","0");
        httpClient.executeMethod(post2);
        PostMethod1.releaseConnection();
        post.releaseConnection();
        get.releaseConnection();
    }
原文地址:https://www.cnblogs.com/xumin/p/3184179.html