Java编程模拟浏览器访问网站及不能访问时播放音乐提醒

我们用人手动来定时检测网站是否打开,太劳民伤财了,不太实际。那么我们就想办法用电脑来做这些枯燥的事,用Java编程模拟浏览器定时访问网站,如果发现网站链接超时,程序会自动播放音乐。我们运行程序就没事了,直到听见音乐时为止。

一、 抓包
Fiddler太牛了,啥也不说,我们直接用它抓包即可。看看HTTP头如下:

GET http://www.xieyincai.com/ HTTP/1.1
Host: www.xieyincai.com
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8

二、 编码

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class CheckURLStatus {
	// 浏览器信息
	static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
	
	// 检测URL打开情况
	public static boolean getURLStatus(final URL url) {
		
		boolean status = false;
		HttpURLConnection httpURLConnection;
		
		try {
			httpURLConnection = (HttpURLConnection)url.openConnection();
			// 参考抓包软件,填写模拟浏览器信息
			httpURLConnection.setRequestProperty("User-Agent", userAgent);
			httpURLConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			httpURLConnection.setRequestProperty("Connection", "keep-alive");
			httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
			// 设置连接超时时间为5秒
			httpURLConnection.setConnectTimeout(5*1000);
			httpURLConnection.connect();
			// 如果连接不超时,但HTTP状态码不是200,我们也认为网站打开失败
			if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) status = true;
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}
	
	// 播放音乐方法
	public static void openJavaPlayer(final File file) {
		
		Sequencer sequencer = null;
		Sequence sequence = null;

		try {
			sequencer = MidiSystem.getSequencer();
			sequence = MidiSystem.getSequence(file);
			sequencer.open();
			sequencer.setSequence(sequence);
			sequencer.start();
		} catch (InvalidMidiDataException | MidiUnavailableException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	// 检测URL打不开时,播放音乐“我的未来不是梦”
	public static void main(String[] args) throws MalformedURLException, InterruptedException {
		// TODO Auto-generated method stub
		
		URL url = new URL("http://www.xieyincai.com");
		File file = new File("我的未来不是梦.mid");
		
		while(true) {
			
			boolean status = CheckURLStatus.getURLStatus(url);
			// 如果失败,再重试一次
			if(status == false) status = CheckURLStatus.getURLStatus(url);
			
			System.out.println(Calendar.getInstance().getTime().toString() + "\t" + status);
			
			// 检测失败,啥也别说,直接播放音乐
			if(status == false) {
				CheckURLStatus.openJavaPlayer(file);
			}
			
			// 睡觉去了,休息20秒再说
			Thread.sleep(20*1000);
		}
	}

}

运行如下:

Mon Jan 15 16:29:09 CST 2018	true
Mon Jan 15 16:29:30 CST 2018	true
Mon Jan 15 16:29:51 CST 2018	true
Mon Jan 15 16:30:12 CST 2018	true
Mon Jan 15 16:30:33 CST 2018	true
Mon Jan 15 16:30:54 CST 2018	true
Mon Jan 15 16:31:16 CST 2018	true
Mon Jan 15 16:31:37 CST 2018	true
Mon Jan 15 16:31:57 CST 2018	true
Mon Jan 15 16:32:18 CST 2018	true

Leave a Reply

Your email address will not be published. Required fields are marked *