发一个自己写的秒杀参与程序,纯分享,无他用。
秒杀:准点开抢,先抢先得。
页面上的操作流程大致是这样的:登录->填写用户资料->等待倒计时->弹出随机问题->填写答案->提交。
前两步人工完成,程序完成从等待倒计时到提交结束的步骤。主要使用httpclient模拟http请求。前期工作主要研究下原站的相关js代码,获得各步骤必要的URL和相关参数。其中还用到了坛子里的fastjson感谢一下
。
主要代码如下,就不做什么解释了。
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";//CookieString 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));// 解析 JSONJSONArray 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 blocke1.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;}现在活动结束了,发上来大家围观一下吧。
透露透露,go共都干了些什么当然。 5 楼 cheney_love 2011-06-30 有验证码的能搞不?