自动登陆网站,并定期获取信息

  最近报了驾校,因为是普通类,所以每次都要自己预约。而且一次只能预约3个小时。消化了这3个小时才能预约下一次。等我消化了这3个小时后,才发现半个月内的都已经被预约完了。没办法,只能自己没事刷刷看,有没有取消的。

  昨天终于刷烦了,干脆写个程序帮我刷,有事没事,我看看结果就行。网上下了一个HttpClient,看看教程搞了一个。

  Fiddle截获Http电文挺好用的

package monitor.main;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class Monitor {
	
	public static HttpClient httpClient = new DefaultHttpClient();
	public static HttpContext httpContext = new BasicHttpContext();
	private static SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss.SSS");

	public static void main(String[] args) throws Exception {
		// 登陆
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("mt0010uid", "320XXXX"));
		params.add(new BasicNameValuePair("mt0010pwd", "XXXX"));
		params.add(new BasicNameValuePair("login.x", "78"));
		params.add(new BasicNameValuePair("login.y", "19"));
		doHttpPost(params, "http://XXXXXXXXX/scripts/XXXXXX.asp");
		// 取得下一页
		doHttpGet("http://XXXXXX/scripts/XXXX.asp");
		
		// 要求进入预约页面
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("xxx1010.x", "137"));
		params.add(new BasicNameValuePair("xxx1010.y", "44"));
		doHttpPost(params, "http://XXXXXXXXX/scripts/XXXXX.asp");
		// 察看空余位置
		while(true) {
			// 取得预约页面
			String result = doHttpGet("http://XXXXXXXXXX/scripts/xxx1010.asp");
			// 分析空余位置
			Matcher matcher = Pattern.compile("kusya").matcher(result);
			matcher.matches();
			// 输出空余数量
			System.out.println(format.format(Calendar.getInstance().getTime()) 
					+ " Matcher Count:" + countMatcher(matcher));
			// 4分钟后刷新
			Thread.sleep(60000*4);
		}
	}
	// 统计匹配次数
	private static int countMatcher(Matcher matcher) {
		int hitCount = 0;
        while (matcher.find()) {
            hitCount++;
        }
        return hitCount;
	}
	// HttpGet
	private static String doHttpGet(String url) throws IOException,
			ClientProtocolException, Exception {
		// do httpget
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = httpClient.execute(httpGet, httpContext);
		return printResponse(response);
	}
	// HttpPost
	private static String doHttpPost(List<NameValuePair> params, String url) 
		throws UnsupportedEncodingException, IOException, ClientProtocolException, Exception {
		UrlEncodedFormEntity form;
		HttpPost httpPost;
		// httppost
		form = new UrlEncodedFormEntity(params, "shift-jis");
		httpPost = new HttpPost(url);
		httpPost.setEntity(form);
		// do 
		HttpResponse response = httpClient.execute(httpPost, httpContext);
		return printResponse(response);
	}
	// 取得应答
	public static String printResponse(HttpResponse pResponse) throws Exception {
		// response
//		System.out.println(pResponse.getStatusLine().getStatusCode());
//		HttpHost target = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
//		System.out.println("Final target: " + target);
		HttpEntity entity = pResponse.getEntity();
		String str = EntityUtils.toString(entity, "shift-jis");
//		System.out.println(str);
//		EntityUtils.consume(entity);
		return str;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值