import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Slf4j
public class HttpUtil {
public static JSONObject request(Method type, String body, String url, Map<String, String> headers, boolean isOverrideHeaders) {
log.info("url:{}", url);
log.info("method:{}", type);
String result = cn.hutool.http.HttpUtil.createRequest(type, url).headerMap(headers, isOverrideHeaders)
.body(body)
.execute().body();
if (ObjectUtil.isEmpty(result)) {
throw new RuntimeException("网络异常");
}
return JSON.parseObject(result);
}
public static JSONObject request(Method type, String body, String url) {
log.info("url:{}", url);
log.info("method:{}", type);
String result = cn.hutool.http.HttpUtil.createRequest(type, url)
.body(body)
.execute().body();
if (ObjectUtil.isEmpty(result)) {
throw new RuntimeException("网络异常");
}
return JSON.parseObject(result);
}```