工具类一(HttpUtils篇)
来源:互联网 发布:centos scp拷贝文件夹 编辑:程序博客网 时间:2024/05/16 01:17
把项目里老大写的好用的工具类分享一下。觉得好用的拿去。
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;
publicstaticfinal 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, Stringcharset)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, HttpEntityrequsetEntity) 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();
}
}
}
- 工具类一(HttpUtils篇)
- Android HttpUtils工具类
- HttpUtils工具类
- httpUtils工具类
- Android HttpUtils-Http请求的工具类
- 基于okhttp3的工具类HttpUtils
- XUtils源码分析之HttpUtils(一)
- 自己封装的联网工具类HttpUtils的使用
- 和异步网络相关的工具类HttpUtils
- 用于解析Json数据的HttpUtils工具类
- Android HttpUtils网络请求工具类的实现
- HttpUtils 用于进行网络请求的工具类(未测试)
- Android开发过程中常用到的工具类HttpUtils,SDCardUtils
- HttpUtils 用于进行网络请求的工具类
- Servlet--HttpUtils类
- HttpUtils
- HttpUtils
- HttpUtils
- [hdu2167]Pebbles(状态压缩dp)
- 把数组中的数字拼接起来组成最小的数
- 可以展开的textview
- JavaScript红宝书笔记:创建对象
- CSS3高级选择器用法
- 工具类一(HttpUtils篇)
- 【Java并发编程】之二十:并发新特性—Lock锁和条件变量(含代码)(r)
- KMP(5)--hdu4300(KMP变形题)
- Minimum Path Sum
- 贴一个基本知识
- jsp页面返回上一页,在两个页面死循环
- 【Java并发编程】之二十一:并发新特性—阻塞队列和阻塞栈(含代码)(r)
- css 派生选择器+子选择器+相邻兄弟选择器
- 前端构建工具gulp的使用教程集合