网络压力测试

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class BaiduReferer {
 public static void main(String[] args) throws UnknownHostException,
   IOException, InterruptedException {
  for (int i = 0; i < 1000; i++) {
   send("");
   Thread.sleep(5000);
  }
 }

 public static void send(String url) throws UnknownHostException,
   IOException {
  Socket socket = new Socket("www.baidu.com", 80);
  StringBuffer sb = new StringBuffer("GET /" + url + " HTTP/1.1");
  sb.append("\r\n");
  sb.append("Host: www.baidu.com");
  sb.append("\r\n");
  sb.append("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 GTB6");
  sb.append("\r\n");
  sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  sb.append("\r\n");
  sb.append("Accept-Language: zh,en;q=0.8,zh-cn;q=0.5,en-us;q=0.3");
  sb.append("\r\n");
  sb.append("Accept-Encoding: gzip,deflate");
  sb.append("\r\n");
  sb.append("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  sb.append("\r\n");
  sb.append("Keep-Alive: 300");
  sb.append("\r\n");
  sb.append("\r\n");

  OutputStream socketOut = socket.getOutputStream();
  socketOut.write(sb.toString().getBytes());
  socket.shutdownOutput(); // 半闭输出流

  if (socket != null) {
   socket.close();
  }
 }
}

展开阅读全文

没有更多推荐了,返回首页