基于httpclient的一个简单刷票器实现

package cn.ustb.edu.bean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Hello {
	public static void main(String[] args) throws Exception {
		File file = new File("G:/aaa.txt");
			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file), "GBK");
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTXT = null;
			String username = "", password = "";
			String split[] = new String[2];
			
			while ((lineTXT = bufferedReader.readLine()) != null) {
				split = lineTXT.toString().trim().split("\\s{1,}");
				username = split[0];
				password = split[1];
				
				// 请求认证借口,获取cookie
				HttpClient client = new HttpClient();
				GetMethod get = new GetMethod(
						"pollurl");
				client.executeMethod(get);
				String html = get.getResponseBodyAsString();
				get.releaseConnection();
				Document document = Jsoup.parse(html);
				Elements element = document.select("input[name=csrf_token]");
				String csrf = element.get(0).attr("value");
				get.releaseConnection();

				PostMethod post1 = new PostMethod(
						"pollurl");
				System.err.println( username+":"+password );
				NameValuePair namePair[] = {
						new NameValuePair("username", username ),
						new NameValuePair("password", password ),
						new NameValuePair("login", "1"),
						new NameValuePair("csrf_token", csrf) };	
				post1.setRequestBody(namePair);
				int state = client.executeMethod(post1);
				CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
				Cookie[] cookies = cookiespec.match("vote.ibeike.com", 80, "",
						false, client.getState().getCookies());
				String coo = "";
				for (Cookie cookie : cookies) {
					System.out.println(cookie.toString());
					coo = cookie.toString();
				}
				post1.releaseConnection();

				PostMethod post2 = new PostMethod(
						"pollurl");
				NameValuePair namePair1[] = {
						new NameValuePair("checked[]", "16"),  // youjia
						new NameValuePair("csrf_token", csrf),
						new NameValuePair("isPoll", "1") };
				post2.setRequestBody( namePair1 );
				state = client.executeMethod(post2);
				
				Thread.sleep(3000);
			}
			read.close();
	}
}


aaa.txt文件里面放置的是用户名和密码,用来登录这个投票网站的,格式大概为: username     password 中间是空格
主要是运用httpclient登录网站,获取Cookie,然后带着Cookie去访问下一个页面,获取相关数据后提交投票请求。
 
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值