java多线程模拟有会话的访问web系统,并记录访问日志
2014-07-17 13:37
465 查看
代码如下:
不要做坏事呦
package pressureTest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Scanner;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpTest implements Runnable {
private static String strUrl = null;
private static long numL = 0L;
private static Scanner scanner = new Scanner(System.in);
private static File f = new File("E:\\test.txt");
private static HttpClient httpClient;
public HttpTest(HttpClient httpClient){
this.httpClient=httpClient;
}
public void run() {
while (true)
try {
// Cookie[] cookies = httpClient.getState().getCookies();
// httpClient.getState().addCookies(cookies);
GetMethod get = new GetMethod(strUrl + numL);
// get.setRequestHeader("Cookie" , cookies.toString());
int statusCode = httpClient.executeMethod(get);
// CloseableHttpClient httpclient = HttpClients.createDefault();
// HttpGet httpget = new HttpGet(strUrl + numL);
// CloseableHttpResponse response = httpclient.execute(httpget);
try {
// int statusCode = response.getStatusLine().getStatusCode();
if(statusCode!=200){
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
bw.write("第"+numL+"次访问,状态码:["+statusCode+"] 时间"+Calendar.getInstance().getTime().toString());
bw.newLine();
bw.close();
break;
}
} finally {
get.releaseConnection();
numL++;
}
Thread.sleep(10L);
} catch (InterruptedException e) {
while (true) {
e.getMessage();
try {
Thread.sleep(1000L);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HttpException, IOException {
HttpClient client = login();
System.out.println("");
int threadNum;
while (true) {
System.out.println("请输入要生成的线程数:");
threadNum = scanner.nextInt();
System.out.println("请输入网址:");
String str = scanner.next();
// String str = "http://www.baidu.com";
if ((!(str.startsWith("http://")))
&& (!(str.startsWith("https://")))) {
strUrl = "http://" + str;
System.out.println(strUrl);
} else {
strUrl = str;
}
if (str.indexOf("?") >= 0)
strUrl += "&num=";
else {
strUrl += "?num=";
}
System.out.println("--------------------------------------");
System.out.println("线程数:" + threadNum);
System.out.println("地址" + str);
System.out.println("请再次确认(Y/N):");
String tmp = scanner.next();
if ("Y".equalsIgnoreCase(tmp))
break;
if ("N".equalsIgnoreCase(tmp)) {
continue;
}
// System.out.println("输入错误,请重新输入(Y/N):");
}
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(f, true));
bw.write("测试开始 时间:"+Calendar.getInstance().getTime().toString());
bw.newLine();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < threadNum; i++) {
HttpTest at = new HttpTest(client);
Thread t = new Thread(at);
t.start();
// System.out.println(i);
}
}
private static HttpClient login() throws HttpException, IOException {
HttpClient client = new HttpClient();
// web系统登录页面 以及登陆信息设置
PostMethod post = new PostMethod(
"http://XXXXXXXXXXXXX");
NameValuePair ie = new NameValuePair("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
NameValuePair url = new NameValuePair("url", "/TelinCRMQW/");
NameValuePair username = new NameValuePair("username", "admin");
NameValuePair password = new NameValuePair("userpassword", "password");
NameValuePair session = new NameValuePair("session", "1111111");
post.setRequestBody(new NameValuePair[] { ie, url, username, password });
client.executeMethod(post);
System.out
.println("******************************登录******************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
return client;
}
}
不要做坏事呦
package pressureTest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Scanner;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpTest implements Runnable {
private static String strUrl = null;
private static long numL = 0L;
private static Scanner scanner = new Scanner(System.in);
private static File f = new File("E:\\test.txt");
private static HttpClient httpClient;
public HttpTest(HttpClient httpClient){
this.httpClient=httpClient;
}
public void run() {
while (true)
try {
// Cookie[] cookies = httpClient.getState().getCookies();
// httpClient.getState().addCookies(cookies);
GetMethod get = new GetMethod(strUrl + numL);
// get.setRequestHeader("Cookie" , cookies.toString());
int statusCode = httpClient.executeMethod(get);
// CloseableHttpClient httpclient = HttpClients.createDefault();
// HttpGet httpget = new HttpGet(strUrl + numL);
// CloseableHttpResponse response = httpclient.execute(httpget);
try {
// int statusCode = response.getStatusLine().getStatusCode();
if(statusCode!=200){
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
bw.write("第"+numL+"次访问,状态码:["+statusCode+"] 时间"+Calendar.getInstance().getTime().toString());
bw.newLine();
bw.close();
break;
}
} finally {
get.releaseConnection();
numL++;
}
Thread.sleep(10L);
} catch (InterruptedException e) {
while (true) {
e.getMessage();
try {
Thread.sleep(1000L);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HttpException, IOException {
HttpClient client = login();
System.out.println("");
int threadNum;
while (true) {
System.out.println("请输入要生成的线程数:");
threadNum = scanner.nextInt();
System.out.println("请输入网址:");
String str = scanner.next();
// String str = "http://www.baidu.com";
if ((!(str.startsWith("http://")))
&& (!(str.startsWith("https://")))) {
strUrl = "http://" + str;
System.out.println(strUrl);
} else {
strUrl = str;
}
if (str.indexOf("?") >= 0)
strUrl += "&num=";
else {
strUrl += "?num=";
}
System.out.println("--------------------------------------");
System.out.println("线程数:" + threadNum);
System.out.println("地址" + str);
System.out.println("请再次确认(Y/N):");
String tmp = scanner.next();
if ("Y".equalsIgnoreCase(tmp))
break;
if ("N".equalsIgnoreCase(tmp)) {
continue;
}
// System.out.println("输入错误,请重新输入(Y/N):");
}
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(f, true));
bw.write("测试开始 时间:"+Calendar.getInstance().getTime().toString());
bw.newLine();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < threadNum; i++) {
HttpTest at = new HttpTest(client);
Thread t = new Thread(at);
t.start();
// System.out.println(i);
}
}
private static HttpClient login() throws HttpException, IOException {
HttpClient client = new HttpClient();
// web系统登录页面 以及登陆信息设置
PostMethod post = new PostMethod(
"http://XXXXXXXXXXXXX");
NameValuePair ie = new NameValuePair("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
NameValuePair url = new NameValuePair("url", "/TelinCRMQW/");
NameValuePair username = new NameValuePair("username", "admin");
NameValuePair password = new NameValuePair("userpassword", "password");
NameValuePair session = new NameValuePair("session", "1111111");
post.setRequestBody(new NameValuePair[] { ie, url, username, password });
client.executeMethod(post);
System.out
.println("******************************登录******************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
return client;
}
}
相关文章推荐
- 在华为实现的java访问https,多线程,写日志
- WEB系统 记录当前用户个数,当前访问系统的用户名 2
- java Web应用配置log4j日志记录
- java多线程之火车售票系统模拟
- JavaWeb商城系统项目记录(一)
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
- 用java多线程操作模拟售票系统
- java多线程模拟停车系统
- javaweb 用户未登入,只能访问系统默认首页。
- java模拟访问web页面
- java 多线程模拟售票系统
- java访问webservce,保持会话,服务端保存session验证
- java火车站售票系统之多线程并发访问
- WEB系统 记录当前用户个数,当前访问系统的用户名
- 两步实现Log4j记录java控制台输出的系统所有异常信息和自定义记录日志---->非常实用,强烈推荐阅读
- 如何用服务器文件管理系统记录共享文件的访问日志、记录共享文件复制、修改、删除和剪切等操作?
- java程序模拟浏览器访问Web服务器的处理过程
- JavaWeb商城系统项目记录(二)
- java 多线程协作 模拟四台电梯的工作状态并统计每层上下楼人数和输出上下楼日志到文件