| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /**
- * Copyright
- * All right reserved.
- * 项目名称:
- * 创建日期: 2022/11/9
- */
- package org.springblade.utils;
- import cn.hutool.core.util.CharsetUtil;
- import cn.hutool.http.ContentType;
- import cn.hutool.http.HttpRequest;
- import cn.hutool.http.HttpResponse;
- import cn.hutool.http.HttpStatus;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springblade.core.log.exception.ServiceException;
- import java.util.Map;
- /**
- * 创建日期: 2022/11/9
- * Title: 文件所属模块(必须填写)
- * Description:对本文件的详细描述,原则上不能少于30字
- * @author DSH
- * @mender:(文件的修改者,文件创建者之外的人)
- * @version 1.0
- * Remark:认为有必要的其他信息
- */
- @Slf4j
- public class HttpRequestUtil {
- /**
- * 功能: GET请求
- * 作者: DSH
- * 创建日期: 2022/11/09
- * @param interfaceName 接口名称
- * @param url 接口地址
- * @param headers 请求头
- * @param paramMap 接口入参
- * @param nTimeOut 超时时间,单位:毫秒
- * @return: com.alibaba.fastjson.JSONObject
- */
- public static JSONObject doHttpGetRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
- log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
- HttpResponse response = HttpRequest.get(url)
- .form(paramMap)
- .addHeaders(headers)
- .timeout(nTimeOut)
- .execute();
- if (response.getStatus() == HttpStatus.HTTP_OK) {
- String body = response.body();
- log.info("【{}】接口响应:{}", interfaceName, body);
- if (StringUtils.isNotBlank(body)) {
- JSONObject result = JSONObject.parseObject(body);
- if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
- throw new ServiceException(result.getString("msg"));
- }
- return result;
- }
- } else {
- log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
- throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
- }
- return null;
- }
- /**
- * 功能: POST请求
- * 作者: DSH
- * 创建日期: 2022/11/09
- * @param interfaceName 接口名称
- * @param url 接口地址
- * @param headers 请求头
- * @param paramMap 接口入参
- * @param nTimeOut 超时时间,单位:毫秒
- * @return: com.alibaba.fastjson.JSONObject
- */
- public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
- log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
- HttpResponse response = HttpRequest.post(url)
- //.contentType("application/json;charset=utf-8")
- .contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
- .body(paramMap == null ? null : JSONObject.toJSONString(paramMap))
- .addHeaders(headers)
- .timeout(nTimeOut)
- .execute();
- if (response.getStatus() == HttpStatus.HTTP_OK) {
- String body = response.body();
- log.info("【{}】接口响应:{}", interfaceName, body);
- if (StringUtils.isNotBlank(body)) {
- JSONObject result = JSONObject.parseObject(body);
- if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
- throw new ServiceException(result.getString("msg"));
- }
- return result;
- }
- } else {
- log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
- throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
- }
- return null;
- }
- /**
- * POST请求 ,按JSON字符串传入BODY
- * @param interfaceName
- * @param url
- * @param headers
- * @param paramMap
- * @param nTimeOut
- * @return
- */
- public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, String paramMap, int nTimeOut) {
- log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
- HttpResponse response = HttpRequest.post(url)
- // .contentType("application/json;charset=utf-8")
- .contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
- .body(paramMap)
- .addHeaders(headers)
- .timeout(nTimeOut)
- .execute();
- if (response.getStatus() == HttpStatus.HTTP_OK) {
- String body = response.body();
- log.info("【{}】接口响应:{}", interfaceName, body);
- if (StringUtils.isNotBlank(body)) {
- JSONObject result = JSONObject.parseObject(body);
- // if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
- // throw new ServiceException(result.getString("msg"));
- // }
- return result;
- }
- } else {
- log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
- throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
- }
- return null;
- }
- /**
- * 功能: POST请求
- * 作者: DSH
- * 创建日期: 2022/11/09
- * @param interfaceName 接口名称
- * @param url 接口地址
- * @param headers 请求头
- * @param paramMap 接口入参
- * @param nTimeOut 超时时间,单位:毫秒
- * @return: com.alibaba.fastjson.JSONObject
- */
- public static JSONObject doHttpPostSubmitForm(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
- log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
- HttpResponse response = HttpRequest.post(url)
- .form(paramMap)
- .addHeaders(headers)
- .timeout(nTimeOut)
- .execute();
- if (response.getStatus() == HttpStatus.HTTP_OK) {
- String body = response.body();
- log.info("【{}】接口响应:{}", interfaceName, body);
- if (StringUtils.isNotBlank(body)) {
- JSONObject result = JSONObject.parseObject(body);
- if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
- throw new ServiceException(result.getString("msg"));
- }
- return result;
- }
- } else {
- log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
- throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
- }
- return null;
- }
- }
|