使用httpClient模拟登陆校内网并留言

作者: ldsea 分类: 程序生活 发布时间: 2012-05-02 16:05

转自:http://shengmo8611.iteye.com/blog/853034

使用httpClient模拟登陆校内网并留言
首先我使用的是一个叫做iehttpHeader的IE浏览器的插件将前传后打开IE浏览器后点击查看–iehttpHeader浏览器下方就 会出现交互信息了其安装件我已经放在附件里了如果你有火狐的话最好配合火狐debug一起使用因为有些时候iehttpheader得到的头部信息并不十 分的完整..我将我的
一部分头部信息以及代码凉一下,大家见笑了
登陆的头部信息:

 

Java语言高亮代码由发芽网提供
[color=brown]POST /PLogin.do HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword,application/vnd.ms-excel, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, */*
Referer: http://www.renren.com/PLogin.do
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
Host: www.renren.com
Content-Length: 171
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: ick=c7782bec-a19a-4177-a4d8-ea2517415e6c; _r01_=1; _de=25733BD8775D04BB608BA0DF927B991F5212E40F3D18115C

email=cuiqiang8611@163.com&password=0532cuiqiang0532&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&formName=&method=&isplogin=true&submit=%E7%99%BB%E5%BD%95[/color]

 

 

Java语言高亮代码由发芽网提供
HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                CookiePolicy.BROWSER_COMPATIBILITY);
        System.out.println("登录校内网");
        HttpPost post = new HttpPost("http://www.renren.com/PLogin.do");
        Header head1 = new BasicHeader(
                "Accept",
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, */*");
        Header head2 = new BasicHeader("Referer",
                "http://www.renren.com/PLogin.do");
        Header head3 = new BasicHeader("Accept-Language""zh-cn");
        Header head4 = new BasicHeader("Content-Type",
                "application/x-www-form-urlencoded");
        Header head5 = new BasicHeader("UA-CPU""x86");
        Header head6 = new BasicHeader("Accept-Encoding""gzip, deflate");
        Header head7 = new BasicHeader("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)");
        Header head8 = new BasicHeader("Host""www.renren.com");
        Header head9 = new BasicHeader("Connection""Keep-Alive");
        Header head10 = new BasicHeader("Cache-Control""no-cache");
        Header head11 = new BasicHeader(
                "Cookie",
                "ick=c7782bec-a19a-4177-a4d8-ea2517415e6c; _r01_=1; _de=25733BD8775D04BB608BA0DF927B991F5212E40F3D18115C");
        post.addHeader(head1);
        post.addHeader(head2);
        post.addHeader(head3);
        post.addHeader(head4);
        post.addHeader(head5);
        post.addHeader(head6);
        post.addHeader(head7);
        post.addHeader(head8);
        post.addHeader(head9);
        post.addHeader(head10);
        post.addHeader(head11);
        NameValuePair[] namevaluepair = new NameValuePair[] {
                new BasicNameValuePair("email""cuiqiang8611@163.com"),
                new BasicNameValuePair("password""0532cuiqiang0532"),
                new BasicNameValuePair("origURL",
                        "http%3A%2F%2Fwww.renren.com%2Fhome"),
                new BasicNameValuePair("domain""renren.com"),
                new BasicNameValuePair("formName"""),
                new BasicNameValuePair("method"""),
                new BasicNameValuePair("isplogin""true"),
                new BasicNameValuePair("submit""%E7%99%BB%E5%BD%95") };
        post.setEntity(new UrlEncodedFormEntity(namevaluepair, "US-ASCII"));
        HttpResponse response = client.execute(post);
        Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            System.out.println("___" + headers[i].getName() + "___"
                    + headers[i].getValue());
        }
        HttpEntity entity = response.getEntity();
         System.out.println("执行回复 : " + response.getStatusLine());
        if (entity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    entity.getContent()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        }
        System.out.println("提交后的Cookies :");
        Cookie[] cookies = ((AbstractHttpClient) client).getCookieStore()
                .getCookies();
        if (cookies.length == 0) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.length; i++) {
                System.out.println("- " + cookies[i].toString());
            }
        }
我就直接将用户名和密码都不改了…懒得慌..我也是能让大家都能看明白了..希望大家别到我的破窝里去捣乱..偶尔去溜达圈还是很欢迎的
这是登陆
留言就差不多一样了
我只是把代码量出来头部信息就不晒了
Java语言高亮代码由发芽网提供
System.out.println("留言");
        HttpPost postl = new HttpPost("http://gossip.renren.com/gossip.do");
        Header headp1 = new BasicHeader("Accept""text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        Header headp2 = new BasicHeader("Accept-Language""zh-cn,zh;q=0.5");
        Header headp3 = new BasicHeader("Referer",
                "http://gossip.renren.com/?origin=103");
        Header headp4 = new BasicHeader("Content-Type",
                "application/x-www-form-urlencoded");
        Header headp5 = new BasicHeader("UA-CPU""x86");
        Header headp6 = new BasicHeader("Accept-Encoding""gzip, deflate");
        Header headp7 = new BasicHeader("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
        Header headp8 = new BasicHeader("Host""gossip.renren.com");
        Header headp9 = new BasicHeader("Connection""Keep-Alive");
        Header headp10 = new BasicHeader("Cache-Control""no-cache");
        Header headp11 = new BasicHeader(
                "Cookie",
                "_r01_=1; depovince=SD; p=5e630e6448f0e070735a1754c7a647ad6; t=61607b1377e41daefeb7fd16841f6e006; societyguester=61607b1377e41daefeb7fd16841f6e006; id=225417436; xnsid=ed8495d4; XNESSESSIONID=3d1ffd9c5b0f; WebOnLineNotice_225417436=1; _urm_225417436=21; JSESSIONID=abcPFWs5fM-INdTCbFs0s");
        // Header headp12=new BasicHeader("X_REQUESTED_WITH", "XMLHttpRequest");
        postl.addHeader(headp1);
        postl.addHeader(headp2);
        postl.addHeader(headp3);
        postl.addHeader(headp4);
        postl.addHeader(headp5);
        postl.addHeader(headp6);
        postl.addHeader(headp7);
        postl.addHeader(headp8);
        postl.addHeader(headp9);
        postl.addHeader(headp10);
        postl.addHeader(headp11);
        // postl.addHeader(headp12);

        NameValuePair[] namevaluepairp = new NameValuePair[] {
                new BasicNameValuePair("body""小强无敌……."),
                new BasicNameValuePair("id""225417436"),
                new BasicNameValuePair("cc""225417436"),
                new BasicNameValuePair("headUrl"null),
                new BasicNameValuePair("largeUrl"null),
                new BasicNameValuePair("requestToken""1638050254"),
                new BasicNameValuePair("only_to_me""0"),
                new BasicNameValuePair("color"null),
                new BasicNameValuePair("ref",
                        "http://gossip.renren.com/"),
                new BasicNameValuePair("mode",null),
                new BasicNameValuePair("requestToken","374492634")
        };
        postl.setEntity(new UrlEncodedFormEntity(namevaluepairp, "UTF-8"));
        response = client.execute(postl);
        entity = response.getEntity();
        System.out.println("执行回复 : " + response.getStatusLine());
        if (entity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    entity.getContent()));
            String line = null;
            while ((line = reader.readLine()) != null) {
        //        System.out.println(line);
            }
            reader.close();
        }
        System.out.println("提交后的Cookies :");
        cookies = null;
        cookies = ((AbstractHttpClient) client).getCookieStore().getCookies();
        if (cookies.length == 0) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.length; i++) {
                System.out.println("- " + cookies[i].toString());
            }

        }

发表评论

电子邮件地址不会被公开。 必填项已用*标注