package com.common.entity.utils;
import okhttp3.*;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;
public class HttpUtil {
private static OkHttpClient client;
private static final String DEFAULT_MEDIA_TYPE = "application/json; charset=utf-8";
private static final int CONNECT_TIMEOUT = 15;
private static final int READ_TIMEOUT = 17;
private static final String GET = "GET";
private static final String POST = "POST";
private static OkHttpClient getInstance() {
if (client == null) {
synchronized (OkHttpClient.class) {
if (client == null) {
client = new OkHttpClient.Builder()
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.build();
}
}
}
return client;
}
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"messages\":[{\"from\":\"bamboo8\",\"destinations\":[{\"to\":\"0086 17717299170\"}],\"text\":\"1234 is your verification code\"}]}");
Request request = new Request.Builder()
.url("https://qgk983.api.infobip.com/sms/2/text/advanced")
.method("POST", body)
.addHeader("Authorization", "Basic cWlhbmd5dTpxdWFkdGFsZW50QEI4")
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
String result = responseBody.string();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String doGet(String url) throws Exception{
try {
long startTime = System.currentTimeMillis();
Request request = new Request.Builder().url(url).build();
String proxyHost = "127.0.0.1";
String proxyPort = "7890";
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
Response response = getInstance().newCall(request).execute();
int httpCode = response.code();
String result;
ResponseBody body = response.body();
if (body != null) {
result = body.string();
addResponseLog(httpCode, result, startTime);
} else {
throw new Exception("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
throw new Exception("http请求异常");
}
}
public static String doPost(Request request) throws Exception{
try {
long startTime = System.currentTimeMillis();
Response response = getInstance().newCall(request).execute();
int httpCode = response.code();
String result;
ResponseBody body = response.body();
if (body != null) {
result = body.string();
addResponseLog(httpCode, result, startTime);
} else {
throw new Exception("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
throw new Exception("http请求异常");
}
}
private static void addResponseLog(int httpCode, String result, long startTime) {
long endTime = System.currentTimeMillis();
}
private static String handleHttpResponse(int httpCode, String result) throws Exception {
if (httpCode == HttpStatus.OK.value()) {
return result;
}
throw new Exception("invalid_token");
}
private static void handleHttpThrowable(Exception ex, String url) throws Exception {
if (ex instanceof Exception) {
throw (Exception) ex;
}
if (ex instanceof SocketTimeoutException) {
throw new RuntimeException("request time out of OkHttp when do url:" + url);
}
throw new RuntimeException(ex);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?