1.背景
相信大家在开发过程中都会遇到,要去请求另一个系统的url,从而去获取信息。当然如果是本系统的url,用这个工具就不适合,直接调用服务就好了。
2.工具类
package org.fh.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HttpRequestUtil {
public static String doGet(String url, Map<String, String> params) {
//构造HttpClient实例
HttpClient client = new HttpClient();
//拼接参数
String paramStr = "";
for (String key : params.keySet()) {
paramStr = paramStr + "&" + key + "=" + params.get(key);
}
paramStr = paramStr.substring(1);
GetMethod method = null;
//接收返回结果
String result = null;
try {
//创建GET方法的实例
method = new GetMethod(url + "?" + paramStr);
//执行HTTP GET方法请求
client.executeMethod(method);
//返回处理结果
result = method.getResponseBodyAsString();
if (result.contains("error")) {
result = null;
}
} catch (Exception e) {
// 发生网络异常
e.printStackTrace();
return null;
} finally {
//释放链接
if (method != null) {
method.releaseConnection();
}
//关闭HttpClient实例
if (client != null) {
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
client = null;
}
}
return result;
}
public static String doGet(String url) {
//构造HttpClient实例
HttpClient client = new HttpClient();
GetMethod method = null;
//接收返回结果
String result = null;
try {
//创建GET方法的实例
method = new GetMethod(url);
//执行HTTP GET方法请求
client.executeMethod(method);
//返回处理结果
result = method.getResponseBodyAsString();
if (result.contains("error")) {
result = null;
}
} catch (Exception e) {
// 发生网络异常
e.printStackTrace();
return null;
} finally {
//释放链接
if (method != null) {
method.releaseConnection();
}
//关闭HttpClient实例
if (client != null) {
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
client = null;
}
}
return result;
}
public static String doPost(String url, Map<String, Object> params) {
HttpClient httpClient = new HttpClient();
//创建POST方法的实例
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("Connection", "close");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
postMethod.addParameter("params", JSONObject.toJSONString(params));
//使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
//接收处理结果
String result = null;
try {
//执行Http Post请求
httpClient.executeMethod(postMethod);
//返回处理结果
result = postMethod.getResponseBodyAsString();
if (result.contains("error")) {
result = null;
}
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
result = null;
} finally {
//释放链接
postMethod.releaseConnection();
//关闭HttpClient实例
if (httpClient != null) {
((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
httpClient = null;
}
}
return result;
}
public static String doPost(String url,Map<String,String> headers, Map<String, Object> params) {
HttpClient httpClient = new HttpClient();
//创建POST方法的实例
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("Connection", "close");
if(headers != null) {
Iterator<Entry<String, String>> entries = headers.entrySet().iterator();
while(entries.hasNext()){
Entry<String, String> entry = entries.next();
postMethod.addRequestHeader(entry.getKey(), entry.getValue());
}
}
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
postMethod.addParameter("params", JSONObject.toJSONString(params));
//使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
//接收处理结果
String result = null;
try {
//执行Http Post请求
httpClient.executeMethod(postMethod);
//返回处理结果
result = postMethod.getResponseBodyAsString();
if (result.contains("error")) {
result = null;
}
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
result = null;
} finally {
//释放链接
postMethod.releaseConnection();
//关闭HttpClient实例
if (httpClient != null) {
((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
httpClient = null;
}
}
return result;
}
public static String doPost(String url,Map<String,String> headers,Map<String,String> paramsEntity,Map<String,String> paramsJson ) {
HttpClient httpClient = new HttpClient();
//创建POST方法的实例
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("Connection", "close");
if(headers != null) {
Iterator<Entry<String, String>> entries = headers.entrySet().iterator();
while(entries.hasNext()){
Entry<String, String> entry = entries.next();
postMethod.addRequestHeader(entry.getKey(), entry.getValue());
}
}
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
postMethod.addParameter("params", JSONObject.toJSONString(paramsJson));
for(String key:paramsEntity.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
postMethod.addParameter(key,paramsEntity.get(key).toString());
}
//使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次
httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
//接收处理结果
String result = null;
try {
//执行Http Post请求
httpClient.executeMethod(postMethod);
//返回处理结果
result = postMethod.getResponseBodyAsString();
if (result.contains("error")) {
result = null;
}
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
result = null;
} finally {
//释放链接
postMethod.releaseConnection();
//关闭HttpClient实例
if (httpClient != null) {
((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
httpClient = null;
}
}
return result;
}
//方法功能描述: 判断是否是IE浏览器
public static boolean isMSBrowser(HttpServletRequest request) {
String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
String userAgent = request.getHeader("User-Agent");
for (String signal : IEBrowserSignals) {
if (userAgent.contains(signal)){
return true;
}
}
return false;
}
}
3.例子
这是要去请求get的url,我们用浏览器看一下结果
利用该工具类去请求用JSONObject.parseObject去解析成功