package Step1; import java.util.Calendar; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; /** * * @ClassName: quhao * @Description: 实验室抢号神器 * @author 泽***ate 2015年11月10日 下午2:51:31 * */ public class quhao { private static int hour; private static int minute; private static int second; private static Calendar c; private static String Url = "http://219.229.132.37:8080/AjaxService/qh.ashx"; private static String cookie = "ASP.NET_SessionId=12pedxyggbpf3x2v1kg0xv55";// 预先设定cookie值 private static int sleeptime = 10;// 设置刷新频率,单位毫秒 public static void main(String[] args) throws InterruptedException { // 等待抢票时间,刷新时间为60S while (true) { setTime();// 获取当前时间 // 开始抢票时间8:18或11:58 if (hour == 8 && minute >= 18 || hour == 11 && minute >= 58) break; System.out.println(hour + ":" + minute + ":" + second + " 现在不是抢号时间段。请稍后..."); Thread.sleep(30000);// 30S刷新时间 } int count = 1; // 开始抢票 while (true) { setTime(); System.out.println("当前取号次数" + count + "次 " + hour + ":" + minute + ":" + second); func(); Thread.sleep(sleeptime); // 请求时间间隔 count++; if (hour == 8 && minute >= 25 || hour == 12 && minute >= 10) { System.out.println("此轮抢号结束"); break; } } } private static void func() { HttpClient httpClient = new HttpClient(); try { GetMethod getMethod = new GetMethod(Url); getMethod.getParams().setContentCharset("utf-8"); // 每次访问需授权的网址时需 cookie 作为通行证 getMethod.setRequestHeader("cookie", cookie); getMethod.setRequestHeader("Accept-Language", "zh-cn"); getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) QQBrowser/9.2.5063.400"); int statusCode = httpClient.executeMethod(getMethod);// 返回状态码200为成功,500为服务器端发生运行错误 System.out.println("返回状态码:" + statusCode); // 打印出返回数据,检验一下是否成功 String result = getMethod.getResponseBodyAsString(); if (statusCode == 500) System.out.println("服务器发生错误"); else System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } private static void setTime() {// 获取当前时间 c = Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE); second = c.get(Calendar.SECOND); } }
Python版:
# -*- coding:utf-8 -*- import urllib2 import time def quhao(): url="http://219.229.132.37:8080/AjaxService/qh.ashx" send_headers={"Accept":"*/*", "Referer": "http://219.229.132.37:8080/Default.aspx", "Accept-Language": "zh-cn", "Accept-Encoding": "gzip, deflate", "User-Agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) QQBrowser/9.2.5063.400", "Host": "219.229.132.37:8080", } cookies= "ASP.NET_SessionId=ff3uelme0mj1uwnqcfegfrup; " send_headers['Cookie']=cookies req = urllib2.Request(url,headers=send_headers) r = urllib2.urlopen(req) html = r.read() html = html.decode('utf-8','replace') return html print quhao() timestruct=time.localtime() while(not ((timestruct.tm_hour==8 and timestruct.tm_min>17 and timestruct.tm_min<25) or ((timestruct.tm_hour==11 and timestruct.tm_min>57) or (timestruct.tm_hour==12 and timestruct.tm_min<5)))): print u"还未到抢票时间" print time.strftime('%Y-%m-%d %H:%M:%S') time.sleep(60) timestruct=time.localtime() timestruct=time.localtime() count=1 if((timestruct.tm_hour==8 and timestruct.tm_min>17 and timestruct.tm_min<25) or ((timestruct.tm_hour==11 and timestruct.tm_min>58) or (timestruct.tm_hour==12 and timestruct.tm_min<5))): while(True): try: html=quhao() print html time.sleep(0.3) print u"正在尝试第"+str(count)+u"次取票" count=count+1 if count>1000: input_raw() except Exception , e: print e else: print u"不在取票时间段内"