浏览 5379 次
锁定老帖子 主题:分享一下自己写的简单的通用http测试工具
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-07-01
最后修改:2009-07-01
晕要发到博客上频道的怎么发到了这里。。。管理员能帮忙移动一下吗 會議系統的优化有了改进,谢谢大家的建议,同时分享一下我写的http通用请求测试工具。保持session是这类测试工具的难点,因为你要进行一系列的操作,比如登录---建立会议---删除会议---登出,你每次请求都要保证你hold住前一次请求的sessionId,我这里是用cookie 携带上一次访问的sessionId, 分享一下我做的通用http请求测试工具,可以自己携带参数比如用户名,密码等等,封装到Map中。 以下可以模拟单线程请求。还可以改装一下,变成多线程的并发,可以分析每次的返回结果,测试时间和返回码(200or404?). import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import com.avonaco.testPojo.GeneralUrlResult; public class HttpRequestProxy { private static int connectTimeOut = 15000; private static int readTimeOut = 20000; private static String requestEncoding = "UTF-8"; private static String JESSIONID; public static GeneralUrlResult doPost(String reqUrl, Map parameters, String recvEncoding){ //统计测试开始结束时间 Long begin=System.currentTimeMillis(); Long end=System.currentTimeMillis(); GeneralUrlResult gr=new GeneralUrlResult(); HttpURLConnection url_con = null; String responseContent = null; int code=0; try { StringBuffer params = new StringBuffer(); for (Iterator iter = parameters.entrySet().iterator(); iter .hasNext();) { Entry element = (Entry) iter.next(); params.append(element.getKey().toString()); params.append("="); params.append(URLEncoder.encode(element.getValue().toString(), HttpRequestProxy.requestEncoding)); params.append("&"); } if (params.length() > 0) { params = params.deleteCharAt(params.length() - 1); } URL url = new URL(reqUrl); url_con = (HttpURLConnection) url.openConnection(); url_con.setUseCaches(false); url_con.setRequestMethod("GET"); if(JESSIONID==null) { //这里的requUrl可以写成网站的通用,比如登录的地址以后每次测试都用这个sessionId保持测试的连贯性 JESSIONID=testUrlConnection("http://192.168.9.225:8080/Conference/login.do?userName=luyi&&password=123456&&loginType=WEB"); } System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>123456"+JESSIONID); url_con.setRequestProperty("Cookie",JESSIONID ); // // System.setProperty("sun.net.client.defaultConnectTimeout", String // .valueOf(HttpRequestProxy.connectTimeOut));// (单位:毫秒)jdk1.4换成这个,连接超时 // System.setProperty("sun.net.client.defaultReadTimeout", String // .valueOf(HttpRequestProxy.readTimeOut)); // (单位:毫秒)jdk1.4换成这个,读操作超时 url_con.setConnectTimeout(12000);//(单位:毫秒)jdk // 1.5换成这个,连接超时 url_con.setReadTimeout(50000);//(单位:毫秒)jdk 1.5换成这个,读操作超时 url_con.setDoOutput(true); byte[] b = params.toString().getBytes(); url_con.getOutputStream().write(b, 0, b.length); url_con.getOutputStream().flush(); url_con.getOutputStream().close(); code=url_con.getResponseCode(); String header; for(int i=0;true;i++){ header=url_con.getHeaderField(i); if(header==null)break; System.out.println("header:"+header); //System.out.println(c.getContentType()); } InputStream in = url_con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(in, recvEncoding)); String tempLine = rd.readLine(); StringBuffer tempStr = new StringBuffer(); String crlf=System.getProperty("line.separator"); while (tempLine != null) { tempStr.append(tempLine); tempStr.append(crlf); tempLine = rd.readLine(); } //得到请求网站返回的内容 responseContent = tempStr.toString(); end=System.currentTimeMillis(); rd.close(); in.close(); } catch (IOException e) { System.out.println(e.toString()); } finally { if (url_con != null) { url_con.disconnect(); } } //得到测试的每次响应时间 Long between=end-begin; //gr是我写的一个简单的结果类,其中有测试时间,返回结果200?or404,还有返回内容 gr.setBetween(between); gr.setCode(code); gr.setResult(responseContent); return gr; } public static int getConnectTimeOut() { return HttpRequestProxy.connectTimeOut; } public static int getReadTimeOut() { return HttpRequestProxy.readTimeOut; } public static String getRequestEncoding() { return requestEncoding; } public static void setConnectTimeOut(int connectTimeOut) { HttpRequestProxy.connectTimeOut = connectTimeOut; } public static void setReadTimeOut(int readTimeOut) { HttpRequestProxy.readTimeOut = readTimeOut; } public static void setRequestEncoding(String requestEncoding) { HttpRequestProxy.requestEncoding = requestEncoding; } public static String testUrlConnection(String Strurl){ String urlStr=Strurl; OutputStream out=null; InputStream in=null; try { URL url=new URL(urlStr); HttpURLConnection con=(HttpURLConnection)url.openConnection(); con.setRequestMethod("GET"); //打印请求头信息 Map hfs=con.getHeaderFields(); Set<String> keys=hfs.keySet(); for(String str:keys){ List<String> vs=(List)hfs.get(str); System.out.print(str+":"); for(String v:vs){ System.out.print(v+"\t"); } System.out.println(); } System.out.println("-----------------------"); String cookieValue=con.getHeaderField("Set-Cookie"); System.out.println("cookie value:"+cookieValue); String sessionId=cookieValue.substring(0, cookieValue.indexOf(";")); return sessionId; } catch (Exception e) { e.printStackTrace(); return null; }finally{ try { if(in!=null) in.close(); if(out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { Map map = new HashMap(); map.put("chart", "555555"); map.put("conId","727622"); GeneralUrlResult temp = HttpRequestProxy.doPost("http://192.168.9.225:8080/Conference/readChart.jsp", map, "UTF-8"); System.out.println("页面为:\n"+temp); } public static String getJESSIONID() { return JESSIONID; } public static void setJESSIONID(String jessionid) { JESSIONID = jessionid; } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-15
缺这个类.GeneralUrlResult
|
|
返回顶楼 | |
发表时间:2010-02-24
中间的注释让我想起 com.hengpeng.common.web.HttpRequestProxy 这个包~
呵呵 |
|
返回顶楼 | |