工具类一(HttpUtils篇)
2016-08-24 16:40
429 查看
把项目里老大写的好用的工具类分享一下。觉得好用的拿去。
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
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.CloseableHttpResponse;
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;
public class HttpUtilsTest {
//生成httpClient
private
static final CloseableHttpClient
httpClient;
publicstatic
final StringCHAESET =
"utf-8";
static {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(6000) //请求超时时间
.setSocketTimeout(6000) //响应超时时间
.build();
httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
}
public
static String httpGet(String url)
throws Exception {
return httpGet(url,null);
}
public
static String httpGet(String url, Map<String, String>params)
throws Exception {
return httpGet(url,params,
CHAESET);
}
/**
*
* @param url
* @param params
* @param charset
* @return
* @throws Exception
*/
public
static String httpGet(String url, Map<String, String>params, String
charset)throws Exception {
//构造url
if(params !=null && !params.isEmpty()) {
List<NameValuePair>
pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String, String>entry :
params.entrySet()) {
if(entry.getValue() !=null) {
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),charset);
if(url.indexOf("?") != -1) {
url +="&" +
queryString;
}else {
url +="?" +
queryString;
}
}
//httpGet获取请求,返回数据
HttpGet httpGet =new HttpGet(url);
CloseableHttpResponse
response = httpClient.execute(httpGet);
try{
int
statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200) {
httpGet.abort();
thrownew RuntimeException("httpClient: error
status code:" +statusCode);
}
HttpEntity entity =response.getEntity();
String result =null;
if(entity !=null) {
result = EntityUtils.toString(entity,charset);
}
EntityUtils.consume(entity);
returnresult;
}finally {
response.close();
}
}
public
static String httpPost(String url, HttpEntityrequestEntity)
throws Exception {
return httpPost(url,null,
requestEntity);
}
public
static String httpPost(String url, Map<String, String>params, HttpEntity
requsetEntity) throws Exception {
//构建url
if(params !=null && !params.isEmpty()) {
List<NameValuePair>
pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String, String>entry :
params.entrySet()) {
if(entry.getValue() !=null) {
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),CHAESET);
if(url.indexOf("?") > 0) {
url +="&" +
queryString;
}else {
url +="?" +
queryString;
}
}
//httpPost请求获取数据
HttpPost httpPost =new HttpPost(url);
httpPost.setEntity(requsetEntity);
CloseableHttpResponse
response = httpClient.execute(httpPost);
try{
int
statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200) {
httpPost.abort();
thrownew RuntimeException("httpClient error status
code:" +statusCode);
}
HttpEntity entity =response.getEntity();
String result =null;
if(entity !=null) {
result = EntityUtils.toString(entity,CHAESET);
}
EntityUtils.consume(entity);
returnresult;
}finally {
response.close();
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
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.CloseableHttpResponse;
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;
public class HttpUtilsTest {
//生成httpClient
private
static final CloseableHttpClient
httpClient;
publicstatic
final StringCHAESET =
"utf-8";
static {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(6000) //请求超时时间
.setSocketTimeout(6000) //响应超时时间
.build();
httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
}
public
static String httpGet(String url)
throws Exception {
return httpGet(url,null);
}
public
static String httpGet(String url, Map<String, String>params)
throws Exception {
return httpGet(url,params,
CHAESET);
}
/**
*
* @param url
* @param params
* @param charset
* @return
* @throws Exception
*/
public
static String httpGet(String url, Map<String, String>params, String
charset)throws Exception {
//构造url
if(params !=null && !params.isEmpty()) {
List<NameValuePair>
pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String, String>entry :
params.entrySet()) {
if(entry.getValue() !=null) {
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),charset);
if(url.indexOf("?") != -1) {
url +="&" +
queryString;
}else {
url +="?" +
queryString;
}
}
//httpGet获取请求,返回数据
HttpGet httpGet =new HttpGet(url);
CloseableHttpResponse
response = httpClient.execute(httpGet);
try{
int
statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200) {
httpGet.abort();
thrownew RuntimeException("httpClient: error
status code:" +statusCode);
}
HttpEntity entity =response.getEntity();
String result =null;
if(entity !=null) {
result = EntityUtils.toString(entity,charset);
}
EntityUtils.consume(entity);
returnresult;
}finally {
response.close();
}
}
public
static String httpPost(String url, HttpEntityrequestEntity)
throws Exception {
return httpPost(url,null,
requestEntity);
}
public
static String httpPost(String url, Map<String, String>params, HttpEntity
requsetEntity) throws Exception {
//构建url
if(params !=null && !params.isEmpty()) {
List<NameValuePair>
pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String, String>entry :
params.entrySet()) {
if(entry.getValue() !=null) {
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),CHAESET);
if(url.indexOf("?") > 0) {
url +="&" +
queryString;
}else {
url +="?" +
queryString;
}
}
//httpPost请求获取数据
HttpPost httpPost =new HttpPost(url);
httpPost.setEntity(requsetEntity);
CloseableHttpResponse
response = httpClient.execute(httpPost);
try{
int
statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200) {
httpPost.abort();
thrownew RuntimeException("httpClient error status
code:" +statusCode);
}
HttpEntity entity =response.getEntity();
String result =null;
if(entity !=null) {
result = EntityUtils.toString(entity,CHAESET);
}
EntityUtils.consume(entity);
returnresult;
}finally {
response.close();
}
}
}
相关文章推荐
- 简单使用xUtils-HttpUtils
- 开发日记(8) 为什么我的HttpUtils过一分钟才会有数据再刷新?
- HttpUtils
- HttpUtils请求XML文件,XStream解析
- Xutils框架介绍
- Android Xutils 框架的介绍
- Android常用工具类
- XUtils的简单使用
- 我知道的xUtils
- Android 实现 session 保持
- xUtils网络请求时的参数问题
- android-----基于XUtils照片上传客户端以及服务器端实现
- android-----XUtils框架之HttpUtils源码分析
- 项目中Gson解析的上拉加载下拉刷新(HttpUtils)
- HttpUtils.java
- HttpUtils 发送http请求工具类(实例讲解)
- kl距离(散度)&l1范数区别
- TCP/IP详解-TCP连接建立和断开
- Okhttp任务队列工作原理
- 嵌入式实时操作系统http://www.trtos.com/