帮朋友刷网络投票,由于刷投票最大的问题在ip上,家里用的是光猫,所以针对自己家里的这款光猫写了定时刷票功能,写完后拿出来共享。
package vote; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.ArrayList; import org.apache.commons.httpclient.Header; 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.methods.PostMethod; import org.apache.commons.httpclient.Header; public class SendPostMethod { public String methodPost(String url,NameValuePair[] data){ String response= "";//要返回的response信息 HttpClient httpClient = new HttpClient(); List<Header> headers = new ArrayList<Header>(); headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon;)")); //qq投票判断referer,不然投票无效 headers.add(new Header("referer", "http://xxx.qq.com/zt2011/matchofgym/index.htm")); httpClient.getHostConfiguration().getParams().setParameter( "http.default-headers", headers); PostMethod postMethod = new PostMethod(url); // 将表单的值放入postMethod中 postMethod.setRequestBody(data); // 执行postMethod int statusCode = 0; try { statusCode = httpClient.executeMethod(postMethod); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发 // 301或者302 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 从头中取出转向的地址 Header locationHeader = postMethod.getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); System.out.println("The page was redirected to:" + location); response= methodPost(location,data);//用跳转后的页面重新请求。 } else { System.err.println("Location field value is null."); } } else { System.out.println(postMethod.getStatusLine()); try { response= postMethod.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); } postMethod.releaseConnection(); } return response; } public void sendVote(){ String url = "http://input.vote.qq.com/survey.php"; NameValuePair pjtId = new NameValuePair("PjtID", "1234567"); NameValuePair result = new NameValuePair("result", "0"); NameValuePair sbj =new NameValuePair("sbj_1234567[]", "123456"); NameValuePair[] data = {pjtId, result, sbj}; String response = methodPost(url,data); System.out.println("sendVote********"+response); } public void loginInRouter() throws IOException{ String url = "http://192.168.1.1"; NameValuePair username = new NameValuePair("Username", "useradmin"); NameValuePair password = new NameValuePair("Password", "prdih"); //获得光猫的token,验证正确才能登录 NameValuePair token = new NameValuePair("Frm_Logintoken", Router.findLoginToken(url)); NameValuePair[] data = {username, password, token}; String response = methodPost(url, data); System.out.println("loginInRouter********"+response); } public void resetRouter() throws IOException{ String url = "http://192.168.1.1/manager_dev_conf_t.gch"; NameValuePair action = new NameValuePair("IF_ACTION", "devrestart"); NameValuePair flag = new NameValuePair("flag", "1"); NameValuePair[] data = {action, flag}; String response = methodPost(url, data); System.out.println("resetRouter********"+response); } public static void main(String[] args) throws IOException,UnsupportedEncodingException { SendPostMethod sm = new SendPostMethod(); //sm.sendVote(); sm.loginInRouter(); sm.resetRouter(); } }
这是模拟表单提交代码,是刷ip和投票功能的核心部分。
package vote; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; import java.util.regex.Pattern; import java.util.regex.Matcher; import org.apache.commons.lang3.StringUtils; public class Router { private static final String regex = "\"\\d+\";"; public static String findLoginToken(String url) throws IOException{ Document doc = Jsoup.connect(url).get(); String content = doc.outerHtml(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(content); String result = ""; if(m.find()){ result = m.group(); result = StringUtils.remove(result, "\""); result = StringUtils.remove(result, ";"); } return result; } public static void main(String[] args) throws IOException{ String routerAddress = "http://192.168.1.1"; String content = Router.findLoginToken(routerAddress); System.out.println("token id: " + content + " ."); } }
中兴F420的机器登录时需要token验证,所以一定要获得token才能通过登录。
以上代码有部分是直接拿网上的源码修改而成的,投票和定时重启光猫是自己写的,由于重启光猫和分配ip时间比较长,所以一般每3分钟才能投一次票,傍晚下班的时候可能需要4分钟一次,需要做出一定调整。包括定时器的源码都放在附件里供大家下载。