ITeye博客

Socket访问网页

txazo · 2012-09-01 11:36 · J2SE · 编程语言 · Markdown

网页URL:http://txazo.iteye.com/blog/1669205 IP地址:118.186.66.50 端口号:80


public class HttpSender {

	private static byte[] request = null;

	static {
		StringBuffer temp = new StringBuffer();
		temp.append("GET http://txazo.iteye.com/blog/1669205 HTTP/1.1\r\n"
            );
        temp.append("Connection: keep-alive\r\n");
		temp.append("Cache-Control: max-age=0\r\n");
		temp.append("\r\n");
		request = temp.toString().getBytes();
	}

	public static void sendHttpRequest() throws Exception {
		Socket socket = new Socket("118.186.66.50", 80);
		OutputStream os = socket.getOutputStream();
		os.write(request);
		os.flush();
		os.close();
		socket.close();
	}

}


public class HttpThread implements Runnable {

	@Override
	public void run() {
		try {
			HttpSender.sendHttpRequest();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


public class HttpClient {

	public static void main(String[] args) throws Exception {
		for (int j = 0; j < 10; j++) {
			for (int i = 0; i < 20; i++) {
				new Thread(new HttpThread()).start();
				System.out.println(20 * j + i);
			}
			Thread.sleep(1000 * 10);
		}
	}

}

这种方法可以快速提升网站的访问量。

评论(1)

  1. #1 楼 · azvf · 2014-02-27 11:55

    这个和httpClient访问有啥区别

评论功能维护中,当前仅展示历史评论。