java HTTP请求工具类

  • @Author: tarzan Liu

  • @Company: 洛阳图联科技有限公司

  • @Date: 2019/10/31 9:11

*/

public class SmartHttpUtil {

/**

  • 方法描述: 发送get请求

  • @param url

  • @param params

  • @param header

  • @Return {@link String}

  • @throws

  • @date 2020年07月27日 09:10:10

*/

public static String sendGet(String url, Map<String, String> params, Map<String, String> header) throws Exception {

HttpGet httpGet = null;

String body = “”;

try {

CloseableHttpClient httpClient = HttpClients.createDefault();

List mapList = new ArrayList<>();

if (params != null) {

for (Entry<String, String> entry : params.entrySet()) {

mapList.add(entry.getKey() + “=” + entry.getValue());

}

}

if (CollectionUtils.isNotEmpty(mapList)) {

url = url + “?”;

String paramsStr = StringUtils.join(mapList, “&”);

url = url + paramsStr;

}

httpGet = new HttpGet(url);

httpGet.setHeader(“Content-type”, “application/json; charset=utf-8”);

httpGet.setHeader(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);

if (header != null) {

for (Entry<String, String> entry : header.entrySet()) {

httpGet.setHeader(entry.getKey(), entry.getValue());

}

}

HttpResponse response = httpClient.execute(httpGet);

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {

throw new RuntimeException(“请求失败”);

} else {

body = EntityUtils.toString(response.getEntity(), “UTF-8”);

}

} catch (Exception e) {

throw e;

} finally {

if (httpGet != null) {

httpGet.releaseConnection();

}

}

return body;

}

/**

  • 方法描述: 发送post请求-json数据

  • @param url

  • @param json

  • @param header

  • @Return {@link String}

  • @throws

  • @date 2020年07月27日 09:10:54

*/

public static String sendPostJson(String url, String json, Map<String, String> header) throws Exception {

HttpPost httpPost = null;

String body = “”;

try {

CloseableHttpClient httpClient = HttpClients.createDefault();

httpPost = new HttpPost(url);

httpPost.setHeader(“Content-type”, “application/json; charset=utf-8”);

httpPost.setHeader(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);

if (header != null) {

for (Entry<String, String> entry : header.entrySet()) {

httpPost.setHeader(entry.getKey(), entry.getValue());

}

}

StringEntity entity = new StringEntity(json, Charset.forName(“UTF-8”));

entity.setContentEncoding(“UTF-8”);

entity.setContentType(“application/json”);

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {

throw new RuntimeException(“请求失败”);

} else {

body = EntityUtils.toString(response.getEntity(), “UTF-8”);

}

} catch (Exception e) {

throw e;

} finally {

if (httpPost != null) {

httpPost.releaseConnection();

}

}

return body;

}

/**

  • 方法描述: 发送post请求-form表单数据

  • @param url

  • @param json

  • @param header

  • @Return {@link String}

  • @throws

  • @date 2020年07月27日 09:10:54

*/

public static String sendPostForm(String url, Map<String, String> params, Map<String, String> header) throws Exception {

HttpPost httpPost = null;

String body = “”;

try {

CloseableHttpClient httpClient = HttpClients.createDefault();

httpPost = new HttpPost(url);

httpPost.setHeader(“Content-type”, “application/x-www-form-urlencoded”);

httpPost.setHeader(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);

if (header != null) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值