帮朋友刷网络投票,由于刷投票最大的问题在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分钟一次,需要做出一定调整。包括定时器的源码都放在附件里供大家下载。
分享到:
相关推荐
标题“中兴F420光猫定时重启换IP刷投票”所涉及的知识点主要集中在网络设备管理、网络编程和自动化脚本上。这可能是关于如何利用特定的中兴F420光猫设备,通过编写脚本来实现定时重启,并在每次重启后更换设备的IP...
电磁场与电磁波28
在使用 R 语言进行科研绘图时,颜色的选择是一件让人特别纠结的事情。本系列文章介绍了 R 语言科研绘图时常用的一些配色。本资源给大家提供了文章对应的 PPT。
Modbus TCP/IP:基于以太网和TCP/IP协议栈,将Modbus协议封装在TCP/IP协议中,适用于通过网络进行远程通信的场合,是目前工业以太网中常用的通信协议之一。 本资源简单实现了如何在在C#的Winform应用程序中使用NModbus库实现Modbus通讯,包括读取保持寄存器、以及相应的UI界面设计和事件处理。 在Modbus网络中,有一个主设备(通常是控制器或上位机)和多个从设备(如传感器、执行器等)。主设备发起通信请求,从设备根据请求进行响应,从设备不能主动向主设备发送数据。 Modbus 从站(Slave)是 Modbus 通信协议中的响应设备,负责接收并执行来自主站(Master)的请求。 Modbus 从站是被动设备,它不能主动发起通信,只能响应主站的请求。从站的主要功能包括: 1、接收主站的指令(如读取数据或执行操作)。 2、执行相应的操作(如读取寄存器值或设置参数)。 3、返回响应数据或错误码给主站。
Java项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文
电磁场与电磁波13