http请求方式-OkHttpClient
http请求方式-OkHttpClient
import com.example.core.mydemo.http.OrderReqVO; import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.CollectionUtils; import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.concurrent.TimeUnit; public class OkHttpClient { private static Logger log = LoggerFactory.getLogger(OkHttpClient.class); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private volatile static okhttp3.OkHttpClient client; private static final int MAX_IDLE_CONNECTION = 2; private static final long KEEP_ALIVE_DURATION = 2; private static final long CONNECT_TIMEOUT = 1000; private static final long READ_TIMEOUT = 3000; /** * 单例模式(双重检查模式) 获取类实例 * * @return client */ private static okhttp3.OkHttpClient getInstance() { if (client == null) { synchronized (okhttp3.OkHttpClient.class) { if (client == null) { client = new okhttp3.OkHttpClient.Builder() .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS) .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) .connectionPool(new ConnectionPool(MAX_IDLE_CONNECTION, KEEP_ALIVE_DURATION, TimeUnit.MINUTES)) .build(); } } } return client; } public static String syncPost(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); try { Response response = OkHttpClient.getInstance().newCall(request).execute(); if (response.isSuccessful()) { String result = response.body().string(); log.info("syncPost response = {}, responseBody= {}", response, result); return result; } String result = response.body().string(); log.info("syncPost response = {}, responseBody= {}", response, result); throw new IOException("三方接口返回http状态码为" + response.code()); } catch (Exception e) { log.error("syncPost() url:{} have a ecxeption {}", url, e); throw new RuntimeException("syncPost() have a ecxeption {}" + e.getMessage()); } } public static String syncGet(String url, Map<String, Object> headParamsMap) throws IOException { Request request; final Request.Builder builder = new Request.Builder().url(url); try { if (!CollectionUtils.isEmpty(headParamsMap)) { final Iterator<Map.Entry<String, Object>> iterator = headParamsMap.entrySet() .iterator(); while (iterator.hasNext()) { final Map.Entry<String, Object> entry = iterator.next(); builder.addHeader(entry.getKey(), (String) entry.getValue()); } } request = builder.build(); Response response = OkHttpClient.getInstance().newCall(request).execute(); String result = response.body().string(); log.info("syncGet response = {},responseBody= {}", response, result); if (!response.isSuccessful()) { throw new IOException("三方接口返回http状态码为" + response.code()); } return result; } catch (Exception e) { log.error("remote interface url:{} have a ecxeption {}", url, e); throw new RuntimeException("三方接口返回异常"); } } public static void main(String[] args) { OrderReqVO data = new OrderReqVO(); data.setOrderNum("111123333"); data.setServerType("1"); String url = "https://域名/接口名称"; try { String resp = syncPost(url, com.alibaba.fastjson.JSON.toJSONString(data)); System.out.println("resp = " + resp); } catch (IOException e) { e.printStackTrace(); } } }
分类:
http&https请求方式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-10-13 win10系统,磁盘出现惊叹号和一把锁符号如何关闭去掉
2021-10-13 thinkpad t490触摸板失灵解决方法