HttpClient的使用,整理了一个工具类
2016-06-14 23:26
513 查看
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
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
相关文章推荐
- ASP.NET MVC Web API HttpClient简介
- 使用httpclient实现免费的google翻译api
- PHP封装的HttpClient类用法实例
- Android 中HttpURLConnection与HttpClient使用的简单实例
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- Android中HttpURLConnection与HttpClient的使用与封装
- Java利用HttpClient模拟POST表单操作应用及注意事项
- HttpClient 在Java项目中的使用详解
- 基于Java HttpClient和Htmlparser实现网络爬虫代码
- 谈谈HttpClient使用详解
- java实现HttpClient异步请求资源的方法
- spring集成httpclient配置
- 调用图灵机器人接口的问题
- Apache Client使用说明第一章(第一部分)
- volley框架下发送和读取cookie
- 浅谈HttpClient
- HttpClient多线程并发问题
- HttpClient连接池使用demo
- HttpClient不保存、发送Cookie
- HttpClient获取服务器JSON数据问题处理