[工具类]HttpRequestUtil

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去解析成功在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值