import com.google.gson.Gson;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
public class HttpUtils {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
final OkHttpClient client
= new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
public String postJson(String url, HashMap<String, String> map) throws IOException {
if (map == null || StringUtils.isEmpty(url)) {
throw new RuntimeException("url或请求参数不能为空");
}
Gson gson = new Gson();
String json = gson.toJson(map);
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody responseBody = response.body();
if (Objects.isNull(responseBody)) {
throw new RuntimeException("响应体为空");
}
return responseBody.string();
} catch (Exception e) {
logger.error(e.toString());
return "";
}
}
public String postJson(String url, String json) {
if (StringUtils.isEmpty(json) || StringUtils.isEmpty(url)) {
throw new RuntimeException("url或请求参数不能为空");
}
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody responseBody = response.body();
if (Objects.isNull(responseBody)) {
throw new RuntimeException("响应体为空");
}
return responseBody.string();
} catch (Exception e) {
logger.error(e.toString());
return "";
}
}
public String get(String url, HashMap<String, String> queryParamMap) throws IOException {
if (StringUtils.isEmpty(url)) {
throw new RuntimeException("url或请求参数不能为空");
}
HttpUrl.Builder builder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
if (queryParamMap != null && queryParamMap.size() != 0) {
queryParamMap.forEach(builder::addQueryParameter);
}
url = builder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody responseBody = response.body();
if (Objects.isNull(responseBody)) {
throw new RuntimeException("响应体为空");
}
return responseBody.string();
} catch (Exception e) {
logger.error(e.toString());
return "";
}
}
public static void main(String[] args) throws IOException {
HashMap<String, String> map = new HashMap<>(8);
map.put("name", "b");
map.put("schoolNo", "1212b");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性