发一个自己写的秒杀参与程序,纯分享,无他用。

秒杀:准点开抢,先抢先得。
页面上的操作流程大致是这样的:登录->填写用户资料->等待倒计时->弹出随机问题->填写答案->提交。
前两步人工完成,程序完成从等待倒计时到提交结束的步骤。主要使用httpclient模拟http请求。前期工作主要研究下原站的相关js代码,获得各步骤必要的URL和相关参数。其中还用到了坛子里的[color=red]fastjson[/color]感谢一下 :wink: 。

主要代码如下,就不做什么解释了。

Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", easyhttps);

HttpClient httpClient = new HttpClient();
//字符编码
httpClient.getParams().setContentCharset("gbk");

HostConfiguration hc = new HostConfiguration();
hc.setHost("www.uqbook.cn", 80, easyhttps);

String configId = "200011111111444444";
String agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
String refer = "https://www.uqbook.cn/sec_initListInfo.action";
String contentType = "application/x-www-form-urlencoded; charset=UTF-8";
//Cookie
String cookie = "JSESSIONID1=pkfXNQDY0DTKRpch4ngp62hD1LNmcp5Jpn8G2R2LQVylxHcH1hpr!-595124091";

String inputsubje = "";//答案一
String inputArith = "68";//答案二
int i = 1;
boolean isFirstIn = true;

while (true) {

if(isFirstIn){
//进入页面
GetMethod inMethod = new GetMethod("https://www.uqbook.cn/initInfo.action");
// 设置参数
inMethod.setRequestHeader("User-Agent", agent);
inMethod.setRequestHeader("Referer", refer);
inMethod.setRequestHeader("Cookie", cookie);
try {
int codeCode = httpClient.executeMethod(hc, inMethod);

InputStream inResp = inMethod.getResponseBodyAsStream();

//循环读取每一行
BufferedReader reader = new BufferedReader(new InputStreamReader(inResp));
String line = null;
String sysCurrentTime = "";
String secondTt = "";
while ((line = reader.readLine())!=null){
//对读取的数据进行编码转换
line = new String(line.getBytes(),"gb2312");
line = line.trim();
if(line.startsWith("var sysCurrentTime")){
sysCurrentTime = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\""));
}else if(line.startsWith("var secondTt")){
secondTt = line.substring(line.indexOf("'")+1, line.lastIndexOf("'"));
}
//logger.info(line);
if(sysCurrentTime!="" && secondTt!=""){
break;
}
}
reader.close();

logger.info("服务器时间:sysCurrentTime="+sysCurrentTime);

logger.info("本机时间:"+DateUtil.getCurrentDateStr(DateUtil.C_TIME_PATTON_DEFAULT));

logger.info("还没到点...等待"+secondTt+"秒...");
Thread.sleep(Integer.parseInt(secondTt));

} catch (Exception e) {
e.printStackTrace();
logger.error("进入页面得到倒计时间异常:" + e);
continue;//继续来一次
}
}
isFirstIn = false;

// 1、取问题及答案
PostMethod subInfoMethod = new PostMethod("https://www.uqbook.cn/getSubjectInfo.action");
// 设置参数
subInfoMethod.setRequestHeader("User-Agent", agent);
subInfoMethod.setRequestHeader("Referer", refer);
subInfoMethod.setRequestHeader("Cookie", cookie);

subInfoMethod.addParameter("timeStamp", ""+System.currentTimeMillis());
subInfoMethod.addParameter("configId", configId);

try {
int subInfoCode = httpClient.executeMethod(hc, subInfoMethod);
String subInfoResp = subInfoMethod.getResponseBodyAsString();

if (subInfoResp == null || "".equals(subInfoResp)) {
logger.info("还没有发布安全问题及答案,本次退出,继续执行下一次!");
continue;
}

logger.info("安全问题及答案解码:" + EscapeUnescape.unescape(subInfoResp));

// 解析 JSON
JSONArray array = JSON.parseArray(subInfoResp);
JSONObject object = (JSONObject) array.get(0);
String content = object.getString("subjectContent");
inputArith = object.getString("arithmeticAnswer");

content = EscapeUnescape.unescape(content).replace("@", " ");

Parser parser = new Parser();
parser.setInputHTML(content);
parser.elements();
for (NodeIterator it = parser.elements(); it.hasMoreNodes();) {
Node node = it.nextNode();
if (node instanceof TagNode) {
TagNode tag = (TagNode) node;
if ("ttwrsnspysk".equalsIgnoreCase(tag
.getAttribute("id"))) {
inputsubje = tag.toPlainTextString().trim();
}
}
;
}

} catch (Exception e) {
e.printStackTrace();
}

inputArith = "27";

if (inputsubje == null || "".equals(inputsubje)
|| inputArith == null || "".equals(inputArith)) {
logger.info("获安全问题及答案时可能发生异常了,本次退出,继续执行下一次!");
continue;
}

//2、判断 是否参加过
PostMethod judgementSecSalMethod = new PostMethod("https://www.uqbook.cn/judgement.action");
// 设置参数
judgementSecSalMethod.setRequestHeader("User-Agent", agent);
judgementSecSalMethod.setRequestHeader("Referer", refer);
judgementSecSalMethod.setRequestHeader("Cookie", cookie);

judgementSecSalMethod.addParameter("timeStamp", ""+System.currentTimeMillis());
judgementSecSalMethod.addParameter("configId", configId);

try {
int judgementSecSalCode = httpClient.executeMethod(hc, judgementSecSalMethod);
String judgementSecSalResp = judgementSecSalMethod.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}

//3、验证问题和答案是否正确
PostMethod checkMethod = new PostMethod("https://www.uqbook.cn/judgementAnswer.action");
// 设置参数
checkMethod.setRequestHeader("User-Agent", agent);
checkMethod.setRequestHeader("Content-Type", contentType);
checkMethod.setRequestHeader("Referer", refer);
checkMethod.setRequestHeader("Cookie", cookie);

checkMethod.addParameter("timeStamp", ""+System.currentTimeMillis());
String tempInputsubje = inputsubje;

try {
tempInputsubje = URLEncoder.encode(inputsubje, "utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

checkMethod.addParameter("inputsubje", tempInputsubje);
checkMethod.addParameter("inputArith", inputArith);

try {
int checkCode = httpClient.executeMethod(hc, checkMethod);
String checkResp = checkMethod.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}


// 3、提交抢购!
PostMethod submitMethod = new PostMethod("https://www.uqbook.cn/submit.action");
// 设置参数
submitMethod.setRequestHeader("User-Agent", agent);
submitMethod.setRequestHeader("Referer", refer);
submitMethod.setRequestHeader("Cookie", cookie);

submitMethod.addParameter("timeStamp", ""+System.currentTimeMillis());
submitMethod.addParameter("inputsubje", EscapeUnescape.escape(inputsubje));// 编一下码
submitMethod.addParameter("inputArith", inputArith);
submitMethod.addParameter("configId", configId);

try {

int submitCode = httpClient.executeMethod(hc, submitMethod);
String submitResp = submitMethod.getResponseBodyAsString();

JSONArray array = JSON.parseArray(submitResp);
JSONObject object = (JSONObject) array.get(0);
String sign = object.getString("sign");
if ("yes".equals(sign)) {
logger.info("抢购登记成功, 退出程序!");
break;
}
logger.info("抢购登记失败,继续...");

} catch (Exception e) {
logger.error("提交抢购异常:" + e);
}

subInfoMethod.releaseConnection();
submitMethod.releaseConnection();
break;
}


现在活动结束了,发上来大家围观一下吧。 :D
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值