论坛首页 Java企业应用论坛

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

浏览 11241 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-06-30   最后修改:2011-06-30
秒杀:准点开抢,先抢先得。
页面上的操作流程大致是这样的:登录->填写用户资料->等待倒计时->弹出随机问题->填写答案->提交。
前两步人工完成,程序完成从等待倒计时到提交结束的步骤。主要使用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";
//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;
}


现在活动结束了,发上来大家围观一下吧。
   发表时间:2011-06-30  
用这个 https://ladyr.es/ladyrbrowser/ 或者 HtmlUnit 只需要模拟浏览器操作,触发个onclick事件就可以 

ladyrbrowser 有性能问题需要优化 ,HtmlUnit  执行js比较慢。。。


以前做舆情监控的时候就是用的ladyrbrowser(需要js构建Dom) 和 HttpClient
0 请登录后投票
   发表时间:2011-06-30  
Reset 写道
用这个 https://ladyr.es/ladyrbrowser/ 或者 HtmlUnit 只需要模拟浏览器操作,触发个onclick事件就可以 

ladyrbrowser 有性能问题需要优化 ,HtmlUnit  执行js比较慢。。。


以前做舆情监控的时候就是用的ladyrbrowser(需要js构建Dom) 和 HttpClient

透露透露,go共都干了些什么
0 请登录后投票
   发表时间:2011-06-30  
LZ用这个称到东西吗? 

不定时秒杀的那种有效吗?
0 请登录后投票
   发表时间:2011-06-30  
dajian 写道
LZ用这个称到东西吗? 

不定时秒杀的那种有效吗?

当然。
0 请登录后投票
   发表时间:2011-06-30  
有验证码的能搞不?
0 请登录后投票
   发表时间:2011-07-02  
倚地屠蛇 写道
dajian 写道
LZ用这个称到东西吗? 

不定时秒杀的那种有效吗?

当然。


我想知道抢到了神马?
0 请登录后投票
   发表时间:2011-07-02  
同问抢啥的?
0 请登录后投票
   发表时间:2011-07-02  
曾经de迷茫 写道
同问抢啥的?

I9008
0 请登录后投票
   发表时间:2011-07-04  
   
  很受启发,但是将代码拷贝到新建的工程后出现了很多错误,无法测试,能否发一个完整的项目学习一下?十分感谢!
我的邮箱:wangleliu@126.com


0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics