就这么简单。(当然,适当的改写一下,就可以做一个加贴机或者灌水机之类的东东,那是你的爱好,和我无关:-)) 程序全文列在最后,并附上了说明 ------------------------------------------------------------------------------- /**************************************************************** Program: TestServer.java Description: send requests in multiple threads to server to test its responses delayance Author: ariesram Date: Aug 23, 2001 Usage: java TestServer file1 file2 ... file format: URL=[Full URL of form] METHOD=GET|POST|... key1=value1 key2=value2 and so on ... ****************************************************************/ import java.io.*; import java.lang.reflect.Array; import java.net.*; import java.util.*;
public class TestServer { static int loopTimes = 500; public Parameter readFromArgFile(String str){ FileInputStream fileInput; BufferedReader br; Parameter param = new Parameter(); try { fileInput = new FileInputStream(new File(str)); br = new BufferedReader( new InputStreamReader( fileInput ));
String line; while( (line = br.readLine()) != null ) { if( line.startsWith("URL") == true && line.indexOf("=") >= 3) { int f = line.indexOf("="); String urlstring = line.substring(f+1); urlstring.trim(); param.url = new URL(urlstring); } else if( line.startsWith("METHOD") == true && line.indexOf("=") >= 3) { int f = line.indexOf("="); String method = line.substring(f+1); method.trim(); param.method = method; } else if( line.indexOf("=") != -1 ) { int f = line.indexOf("="); String key = line.substring(0, f-1); String value = line.substring(f+1); param.addPair(key.trim(), value.trim()); } } fileInput.close(); br.close(); } catch(FileNotFoundException e) { System.out.println("File " + str + " not found."); } catch(NullPointerException e) { } catch(IOException e) { System.out.println(e); } return param; } public static void main(String[] args) { int i; int j; Parameter param; TestServer tester = new TestServer(); for(i = 0; i < Array.getLength(args); i++) { param = tester.readFromArgFile(args[i]); for(j = 0; j < loopTimes; j++) { Thread th = new Thread(new TestThread(param)); th.start(); } } } } class Parameter { URL url; String[] key; String[] value; String method; int length = 0; public void addPair(String k, String v) { Array.set(key, length, k); Array.set(value, length, v); length++; } } class TestThread implements Runnable { Parameter param; TestThread(Parameter par) { param = par; } public void run() { long time1 = new Date().getTime(); try { URL target = param.url; HttpURLConnection conn = (HttpURLConnection) target.openConnection(); conn.setRequestMethod(param.method); int i; for( i = 0; i < param.length; i++ ) { conn.setRequestProperty(param.key[i], param.value[i]); } conn.connect(); BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String inputLine; while( (inputLine = in.readLine()) != null ); } catch(Exception e) { } long time2 = new Date().getTime(); System.out.println(time2 - time1); } }