junit单元测试工具类
来源:互联网 发布:伊戈达拉身体数据 编辑:程序博客网 时间:2024/05/17 23:47
package org.benpaobang.system.utils;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
*
* @ClassName: HttpClientUtils
* @Description: test测试工具类
* @author llj
* @date 2015年12月2日 下午3:36:22
*
*/
public class HttpClientUtils {
/**
* 默认的编码,解决中文乱码
*/
public static String defaultEncode = "UTF-8";
/**
* 发送Post请求
*
* @param url
* 请求路径
* @param paramMap
* 参数
* @return 响应体
*/
public static String getSendPost(String url, Map<String, String> paramMap) {
return getSendPost(url, paramMap, defaultEncode);
}
/**
* 发送Post请求
*
* @param url
* 请求路径
* @param paramMap
* 参数
* @return 响应体
*/
public static String getSendPost(String url, Map<String, String> paramMap, String encode) {
StringBuffer buf = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
if (paramMap.size() > 0) {
NameValuePair[] params = new NameValuePair[paramMap.size()];
Iterator<Entry<String, String>> it = paramMap.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<String, String> map = (Map.Entry<String, String>) it.next();
params[i] = new NameValuePair(map.getKey(), map.getValue());
i++;
}
postMethod.setQueryString(params); // post请求参数用setQueryString
}
try {
client.executeMethod(postMethod);
byte[] responseBody = postMethod.getResponseBody();
String content = new String(responseBody, encode);
buf.append(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
return buf.toString();
}
/**
* @Title: httpGet
* @Description: HTTP GET 请求
*
* @param url 请求地址
* @return 请求结果
*/
public static String httpGet(String url){
StringBuffer buf = new StringBuffer();
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
client.executeMethod(getMethod);
byte[] responseBody = getMethod.getResponseBody();
String content = new String(responseBody, defaultEncode);
buf.append(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return buf.toString();
}
}
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
*
* @ClassName: HttpClientUtils
* @Description: test测试工具类
* @author llj
* @date 2015年12月2日 下午3:36:22
*
*/
public class HttpClientUtils {
/**
* 默认的编码,解决中文乱码
*/
public static String defaultEncode = "UTF-8";
/**
* 发送Post请求
*
* @param url
* 请求路径
* @param paramMap
* 参数
* @return 响应体
*/
public static String getSendPost(String url, Map<String, String> paramMap) {
return getSendPost(url, paramMap, defaultEncode);
}
/**
* 发送Post请求
*
* @param url
* 请求路径
* @param paramMap
* 参数
* @return 响应体
*/
public static String getSendPost(String url, Map<String, String> paramMap, String encode) {
StringBuffer buf = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
if (paramMap.size() > 0) {
NameValuePair[] params = new NameValuePair[paramMap.size()];
Iterator<Entry<String, String>> it = paramMap.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<String, String> map = (Map.Entry<String, String>) it.next();
params[i] = new NameValuePair(map.getKey(), map.getValue());
i++;
}
postMethod.setQueryString(params); // post请求参数用setQueryString
}
try {
client.executeMethod(postMethod);
byte[] responseBody = postMethod.getResponseBody();
String content = new String(responseBody, encode);
buf.append(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
return buf.toString();
}
/**
* @Title: httpGet
* @Description: HTTP GET 请求
*
* @param url 请求地址
* @return 请求结果
*/
public static String httpGet(String url){
StringBuffer buf = new StringBuffer();
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
client.executeMethod(getMethod);
byte[] responseBody = getMethod.getResponseBody();
String content = new String(responseBody, defaultEncode);
buf.append(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return buf.toString();
}
}
0 0
- junit单元测试工具类
- JUnit单元测试工具
- JUnit单元测试工具
- java单元测试工具--JUnit
- 单元测试工具-JUnit
- Junit 单元测试工具
- java单元测试工具 junit
- 单元测试工具Junit源代码学习
- JUnit单元测试实践:测试工具类和方法(EmptyUtils)
- JUnit单元测试实践:测试工具类和方法(EmptyUtils)
- 【工具及插件】单元测试工具junit
- 软件单元测试工具junit junit4使用帮助
- 使用Junit等工具进行单元测试
- Java单元测试工具——Junit
- Junit —— 单元测试工具基本使用
- java junit (单元测试)测试工具如何使用?
- Junit 单元测试、BeanUtils、Properties类
- Junit 单元测试、BeanUtils、Properties类
- 关于ExpandableListView用法的一个简单小例子
- Android学习资料整理收集--路漫漫其修远兮
- 加载动画生成网站
- JAVA温习:Hibernate六大核心接口,两个主要配置文件
- 彻底征服jQuery 插件开发
- junit单元测试工具类
- [leetcode] 172. Factorial Trailing Zeroes 解题报告
- flume 用户指南 - part 2
- RS232电平 RS485电平 RS422电平 图文详解
- 取得缓存表数据两种方法
- Android如何解析json数组对象
- JS设计模式之构造函数模式
- 导航栏返回按钮的定制
- 正则表达式