java http 封装_Http请求的封装

作为一名程序猴 我是非常的喜欢封装工具类   http请求工具类奉上

com.google.code.gson

gson

2.8.5

org.apache.httpcomponents

httpclient

4.5.10

org.apache.httpcomponents

httpmime

4.5.2

package net.xdclass.xdvideo.utils;

import com.google.gson.Gson;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.config.RequestConfig;

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.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.util.HashMap;

import java.util.Map;

/**

* 封装http get post

*/

public class HttpUtils {

private static final Gson gson = new Gson();

/**

* get方法

* @param url

* @return

*/

public static Map doGet(String url){

Map map = new HashMap<>();

CloseableHttpClient httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //连接超时

.setConnectionRequestTimeout(5000)//请求超时

.setSocketTimeout(5000)

.setRedirectsEnabled(true) //允许自动重定向

.build();

HttpGet httpGet = new HttpGet(url);

httpGet.setConfig(requestConfig);

try{

HttpResponse httpResponse = httpClient.execute(httpGet);

if(httpResponse.getStatusLine().getStatusCode() == 200){

String jsonResult = EntityUtils.toString( httpResponse.getEntity());

map = gson.fromJson(jsonResult,map.getClass());

}

}catch (Exception e){

e.printStackTrace();

}finally {

try {

httpClient.close();

}catch (Exception e){

e.printStackTrace();

}

}

return map;

}

/**

* 封装post

* @return

*/

public static String doPost(String url, String data,int timeout){

CloseableHttpClient httpClient = HttpClients.createDefault();

//超时设置

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //连接超时

.setConnectionRequestTimeout(timeout)//请求超时

.setSocketTimeout(timeout)

.setRedirectsEnabled(true) //允许自动重定向

.build();

HttpPost httpPost = new HttpPost(url);

httpPost.setConfig(requestConfig);

httpPost.addHeader("Content-Type","text/html; chartset=UTF-8");

if(data != null && data instanceof String){ //使用字符串传参

StringEntity stringEntity = new StringEntity(data,"UTF-8");

httpPost.setEntity(stringEntity);

}

try{

CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();

if(httpResponse.getStatusLine().getStatusCode() == 200){

String result = EntityUtils.toString(httpEntity);

return result;

}

}catch (Exception e){

e.printStackTrace();

}finally {

try{

httpClient.close();

}catch (Exception e){

e.printStackTrace();

}

}

return null;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值