最近项目中用到了模拟HTTP请求的部分,下面做了一个小整合,希望能给大家提供一定的参考,如果写的有问题,请帮忙指正,谢谢。
/**
* <设置请求头及请求属性>
* <功能详细描述>
* @param method 请求对象
* @param contentType 内容类型
* @see [类、类#方法、类#成员]
*/
private static void setMethodParams(HttpMethodBase method, String contentType)
{
// 连接类型
method.addRequestHeader("Connection", "keep_live");
// 内容类型
if (!isEmpty(contentType))
{
method.addRequestHeader("Content-Type", contentType);
}
// 设置POST请求超时时间
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, httpRequestTimeout);
// 消息头里带上鉴权
method.addRequestHeader("Authorization", authorization);
}
/**
* <使用POST方式提交HTTP请求>
* <功能详细描述>
* @param address 请求地址
* @param entity 请求参数
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String sendPostRequest(String address, RequestEntity entity)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (isEmpty(address))
{
return "";
}
// 存储服务器的响应消息
String responseMsg = "";
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// POST对象
PostMethod method = new PostMethod(address);
// 设置请求头及属性
setMethodParams(method, null);
// 参数对象
if (null != entity)
{
method.setRequestEntity(entity);
}
// 执行的结果
client.executeMethod(method);
responseMsg = method.getResponseBodyAsString();
logger.info("Response Message Is " + responseMsg);
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
/**
* <使用POST方式提交HTTP请求>
* <包含多个请求参数时使用该方法>
* @param address 请求地址
* @param parts 多个请求参数集合
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String sendPostRequest(String address, NameValuePair[] values)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (isEmpty(address))
{
return "";
}
// 存储服务器的响应消息
String responseMsg = "";
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// POST对象
PostMethod method = new PostMethod(address);
// 设置请求头及属性
setMethodParams(method, "application/x-www-form-urlencoded");
// 参数对象
if (null != values)
{
method.setRequestBody(values);
}
// 执行的结果
client.executeMethod(method);
responseMsg = method.getResponseBodyAsString();
logger.info("Response Message Is " + responseMsg);
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
/**
* <使用POST方式提交HTTP请求>
* <多个请求参数包含文件时使用该方法>
* @param address 请求地址
* @param parts 多个请求参数集合
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String sendPostRequest(String address, Part[] parts)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (isEmpty(address))
{
return "";
}
// 存储服务器的响应消息
String responseMsg = "";
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// POST对象
PostMethod method = new PostMethod(address);
// 设置请求头及属性
setMethodParams(method, null);
// 参数对象
if (null != parts)
{
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
}
// 执行的结果
client.executeMethod(method);
responseMsg = method.getResponseBodyAsString();
logger.info("Response Message Is " + responseMsg);
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
/**
* <使用POST方式提交HTTP请求>
* <包含多个请求参数时使用该方法>
* @param address 请求地址
* @param params 参数字符串
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String sendPostRequest(String address, String params)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (isEmpty(address))
{
return "";
}
// 参数对象
if (null != params)
{
address = address + "?" + params;
}
// 存储服务器的响应消息
String responseMsg = "";
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// POST对象
PostMethod method = new PostMethod(address);
// 设置请求头及属性
setMethodParams(method, "application/x-www-form-urlencoded");
// 执行的结果
client.executeMethod(method);
responseMsg = method.getResponseBodyAsString();
logger.info("Response Message Is " + responseMsg);
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
/**
* <使用GET方式提交HTTP请求>
* <功能详细描述>
* @param address 请求地址
* @param params 请求参数
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String sendGetRequest(String address, String params)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (isEmpty(address))
{
return "";
}
// 存储服务器的响应消息
String responseMsg = "";
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// GET对象
GetMethod method = null;
if (!isEmpty(params))
{
method = new GetMethod(address + "?" + params);
}
else
{
method = new GetMethod(address);
}
// 设置请求头及属性
setMethodParams(method, null);
// 执行的结果
client.executeMethod(method);
responseMsg = method.getResponseBodyAsString();
logger.info("Response Message Is " + responseMsg);
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
/**
* <使用GET方式提交HTTP请求>
* <功能详细描述>
* @param address 请求地址
* @param params 请求参数
* @return 返回请求结果
* @throws HttpException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String[] sendGetRequest(String[] address, String params)
throws HttpException, IOException
{
// 如果请求地址为空直接退出请求
logger.info("Request Address Is " + address);
if (null == address)
{
return null;
}
// 存储响应结果
String[] responseMsg = new String[address.length];
// 客户端对象
HttpClient client = new HttpClient();
// 设置连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
// GET对象
GetMethod method = null;
for (int i = 0; i < address.length; i++)
{
if (!isEmpty(params))
{
method = new GetMethod(address[i] + "?" + params);
}
else
{
method = new GetMethod(address[i]);
}
// 设置请求头及属性
setMethodParams(method, null);
// 执行的结果
client.executeMethod(method);
String result = method.getResponseBodyAsString();
responseMsg[i] = result;
logger.info("Response Message Is " + result);
}
// 释放客户端连接
method.releaseConnection();
// 返回响应结果
return responseMsg;
}
注:authorization字符串的计算方式如下:
/**
* <获取xml文件中的值赋给全局变量>
* <功能详细描述>
* @see [类、类#方法、类#成员]
*/
public static void readConfigXml()
{
try
{
// 获取资源流
InputStream in = Global.class.getResourceAsStream("/config.xml");
// 获取根节点
SAXReader sax = new SAXReader();
sax.setEncoding("UTF-8");
Document doc = sax.read(in);
Element root = doc.getRootElement();
// 路由器用户名
apName = root.elementText("apName");
// 路由器密码
apPwd = root.elementText("apPwd");
// 链接超时时间
httpConnectionTimeout = Integer.parseInt(root.elementText("httpConnectionTimeout")) * 1000;
// 请求超时时间
httpRequestTimeout = Integer.parseInt(root.elementText("httpRequestTimeout")) * 1000;
// 检查次数
checkCount = Integer.parseInt(root.elementText("checkCount"));
// 鉴权字符串
authorization = "Basic " + DatatypeConverter.printBase64Binary((apName + ":" + apPwd).getBytes("UTF-8"));
}
catch (Exception e)
{
logger.error(LOG_EXCEPTION_NAME, e);
}
}