首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

CXF之8(httpClient模拟客户端测试)

2012-11-23 
CXF之八(httpClient模拟客户端测试)?????? CXF服务器端搭建好了以后,我们应该怎么测试,客户端测试方式很多

CXF之八(httpClient模拟客户端测试)

?????? CXF服务器端搭建好了以后,我们应该怎么测试,客户端测试方式很多种,在这里使用HttpClient进行模拟客户端请求测试。由于搭建的CXF支持Rest模式,配置的也都是POST请求方式,所以只列出了HttpClient的POST请求方式的测试类。

?????

?????? 第一步:写一个通用的测试入口类

public class ExpHttpClient {/**本地测试*/    private final static String HTTP_URL="http://localhost:8080/demo/webservice/"; /** 生产测试*/   //private final static String HTTP_URL="http://XXXX:8080/demo/webservice/";         /**连接超时时间*/    private final static int CONN_TIMEOUT=5000;    /**请求超时时间*/    private final static int REQUEST_TIMEOUT=5000;        private final static String RETURN_FOMATE="json";//支持json、xml        /**     * 请求服务器入口   * 创 建 人:  XX     * 创建时间:  2012-11-7 下午03:39:55       * @param methodName 方法名     * @param map  上送输入参数集合     * @return     * @see [类、类#方法、类#成员]     */public String doPost(String methodName,Map<String,String> map){String url = HTTP_URL+methodName;String result =null;    HttpClient httpClient = new HttpClient();    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONN_TIMEOUT);  //连接超时 设置PostMethod postMethod = new PostMethod(url);         postMethod.setRequestBody(this.getPostParams(map));        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,REQUEST_TIMEOUT); //post请求超时 设置     postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");try {                                              int statusCode = httpClient.executeMethod(postMethod);if (statusCode != HttpStatus.SC_OK){//如果请求返回的不是成功,则进行处理return new String(postMethod.getResponseBodyAsString()) ;}else{    result = new String(postMethod.getResponseBodyAsString());}} catch (HttpException e) { System.out.println("Please check your provided http address!"); e.printStackTrace(); return e.getMessage();} catch (IOException e) {   e.printStackTrace();    return e.getMessage();}finally{postMethod.releaseConnection();}return result;} private NameValuePair[] getPostParams(Map<String,String> map){    map.put("_type", RETURN_FOMATE);    NameValuePair[] data =new NameValuePair[map.size()];Iterator<String> it=map.keySet().iterator();int i=0;while(it.hasNext()){String key = it.next();String value =map.get(key);data[i]=new NameValuePair(key,value);i++;}return data;    }}

?第二步:单元测试类

??

public class TestExpGateway {private ExpHttpClient client=null;@Beforepublic void setUp() throws Exception {client = new ExpHttpClient();}@Testpublic void testDosmsSend(){String methodName="smsGateway/doSmsSend";Map<String,String> map =new HashMap<String,String>();map.put("channelNo", "05");map.put("compId", "0");map.put("usePhone", "15100000000"); map.put("content", "消息一");String result=client.doPost(methodName, map);System.out.println(result);}}

?

需要的jar包如下

?

?CXF之8(httpClient模拟客户端测试)

?

热点排行