HttpClient的使用,整理了一个工具类
来源:互联网 发布:数据作用 编辑:程序博客网 时间:2024/06/06 12:51
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* http请求工具类、获取用户端ip工具类
* @author liao.h
*
*/
public class HttpUtil {
/**
* 发送一个post请求
* @param url
* 请求路径
* @param parmsMap
* 发送请求的参数
* @return 请求结果
*/
public static String doPost(String url, Map<String, String> parmsMap) {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
// 配置请求的超时设置
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
httpPost.setConfig(requestConfig);
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : parmsMap.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
// 创建带字符创参数和字符编码的
httpPost.setEntity(entity);
HttpResponse httpResponse;
// post请求
httpResponse = closeableHttpClient.execute(httpPost);
// getEntity()
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
// 打印响应内容
return EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 发送一个get请求
*
* @param url
* 请求路径
* @return 请求结果
*/
public static String doGet(String url) {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json;charset=utf-8");
try {
// 执行get请求
HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 判断响应实体是否为空
if (entity != null) {
String a = EntityUtils.toString(entity);
return a;
}
else {
return httpResponse.getStatusLine().getStatusCode() + "";
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try { // 关闭流并释放资源
closeableHttpClient.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取客户端ip地址
* @param request
* @return ip地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
HttpClient4.0下载地址
http://download.csdn.net/detail/liaohao6/9549724
0 0
- HttpClient的使用,整理了一个工具类
- HTTPClient 工具类的使用
- HttpClient 4.0的使用工具类
- HttpClient的工具类
- 分享一个整理了很多Android开发工具类的链接AndroidUtilCode
- 一个Httpclient工具函数的案例
- httpClient的POST方式使用的工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- Python 获取百度的预览图片
- 几种session存储方式比较
- js api 微信支付 chooseWXPay:fail
- 如何从Eclipse迁移到AndroidStudio
- PYTHON 获取csdn的博客文章标题和描述
- HttpClient的使用,整理了一个工具类
- 跟我一起学JFinal系列一JFinal简介
- 基于余弦相似性的指纹匹配算法在WIFI室内定位上的应用
- HTML5 表单里面的新属性
- Android数据库之ormlite简单使用
- java 实现对象的clone
- js获取当前时间
- 腾讯某论坛存在SSRF漏洞(附批量捡漏脚本) ------ 20160614
- Linux软件管理器(如何使用软件管理器来管理软件)