需要做一個程序
因為java sdk中包含有比較全面的class能夠對http等多種協議的處理方法進行了封裝
需要
a
b
(讀者可以跟隨這一過程
主要的工作都有TestThread來完成
class TestThread implements Runnable {
Parameter param;
TestThread(Parameter par) {
param = par;
}
public void run() {
long time
try {
URL target = param
HttpURLConnection conn = (HttpURLConnection) target
conn
int i;
for( i =
conn
}
nnect();
BufferedReader in = new BufferedReader(
new InputStreamReader(conn
String inputLine;
while( (inputLine = in
}
catch(Exception e) {
}
long time
System
}
}
class TestThread implements Runnable
程序工作如下
在初始化一個TestThread實例的時候
具體程序可以自己看懂
class Parameter {
URL url;
String[] key;
String[] value;
String method;
int length =
public void addPair(String k
Array
Array
length++;
}
}
是用來傳遞參數的一個類
public class TestServer {
static int loopTimes =
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
if( line
int f = line
String urlstring = line
urlstring
param
}
else if( line
int f = line
String method = line
method
thod = method;
}
else if( line
int f = line
String key = line
String value = line
param
}
}
fileInput
br
}
catch(FileNotFoundException e) {
System
}
catch(NullPointerException e) {
}
catch(IOException e) {
System
}
return param;
}
public static void main(String[] args) {
int i;
int j;
Parameter param;
TestServer tester = new TestServer();
for(i =
param = tester
for(j =
Thread th = new Thread(new TestThread(param));
th
}
}
}
}
主程序main也比較簡單
就這麼簡單
程序全文列在最後
/****************************************************************
Program: TestServer
Description: send requests in multiple threads to server to test
its responses delayance
Author: ariesram
Date: Aug
Usage: java TestServer file
file format:
URL=[Full URL of form]
METHOD=GET|POST|
key
key
and so on
****************************************************************/
import java
import java
import
import java
public class TestServer {
static int loopTimes =
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
if( line
int f = line
String urlstring = line
urlstring
param
}
else if( line
int f = line
String method = line
method
thod = method;
}
else if( line
int f = line
String key = line
String value = line
param
}
}
fileInput
br
}
catch(FileNotFoundException e) {
System
}
catch(NullPointerException e) {
}
catch(IOException e) {
System
}
return param;
}
public static void main(String[] args) {
int i;
int j;
Parameter param;
TestServer tester = new TestServer();
for(i =
param = tester
for(j =
Thread th = new Thread(new TestThread(param));
th
}
}
}
}
class Parameter {
URL url;
String[] key;
String[] value;
String method;
int length =
public void addPair(String k
Array
Array
length++;
}
}
class TestThread implements Runnable {
Parameter param;
TestThread(Parameter par) {
param = par;
}
public void run() {
long time
try {
URL target = param
HttpURLConnection conn = (HttpURLConnection) target
conn
int i;
for( i =
conn
}
nnect();
BufferedReader in = new BufferedReader(
new InputStreamReader(conn
String inputLine;
while( (inputLine = in
}
catch(Exception e) {
}
long time
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27192.html