perf:移除hutool的http模块,使用okhttp3
This commit is contained in:
@@ -4,18 +4,18 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.platform.config.ding.bean.*;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.service.TsCacheService;
|
||||
import com.tiesheng.util.service.http.OkHttpUtil;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -69,26 +69,25 @@ public class PlatformDingConfig {
|
||||
url = url + "?access_token=" + accessToken;
|
||||
}
|
||||
|
||||
HttpRequest request;
|
||||
Request request;
|
||||
if (body == null) {
|
||||
request = HttpUtil.createGet(url);
|
||||
request = OkHttpUtil.ofGet(url);
|
||||
} else {
|
||||
request = HttpUtil.createPost(url).body(body.toString());
|
||||
request = OkHttpUtil.ofPost(url, body);
|
||||
}
|
||||
request.header("x-acs-dingtalk-access-token", accessToken);
|
||||
HttpResponse execute = request.execute();
|
||||
|
||||
if (execute.isOk()) {
|
||||
String rawBody = execute.body();
|
||||
DingResponse<T> bean = JSONUtil.toBean(rawBody, typeReference, true);
|
||||
if (!bean.isOk()) {
|
||||
LogFactory.get().info(bean.getErrmsg());
|
||||
request.newBuilder().header("x-acs-dingtalk-access-token", accessToken);
|
||||
try {
|
||||
Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute();
|
||||
if (response.isSuccessful() || response.body() != null) {
|
||||
String rawBody = response.body().string();
|
||||
DingResponse<T> bean = JSONUtil.toBean(rawBody, typeReference, true);
|
||||
bean.setRawBody(rawBody);
|
||||
return bean;
|
||||
}
|
||||
bean.setRawBody(rawBody);
|
||||
return bean;
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
|
||||
return null;
|
||||
return DingResponse.ofError();
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +112,7 @@ public class PlatformDingConfig {
|
||||
Map<String, Object> query = new HashMap<>(10);
|
||||
query.put("appkey", dingConfigBean.getAppKey());
|
||||
query.put("appsecret", dingConfigBean.getAppSecret());
|
||||
String response = HttpUtil.get("https://oapi.dingtalk.com/gettoken", query);
|
||||
String response = OkHttpUtil.get("https://oapi.dingtalk.com/gettoken", query);
|
||||
JSONObject respJson = JSONUtil.parseObj(response);
|
||||
accessToken = respJson.getStr("access_token");
|
||||
TsCacheService.of().put(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(),
|
||||
|
||||
@@ -21,6 +21,19 @@ public class DingResponse<T> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造一个异常对象
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static <T> DingResponse<T> ofError() {
|
||||
DingResponse<T> response = new DingResponse<>();
|
||||
response.setErrcode("-1");
|
||||
response.setErrmsg("网络访问异常。");
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从原始数据中获取值
|
||||
*
|
||||
|
||||
@@ -2,12 +2,11 @@ package com.tiesheng.platform.config.wxmini;
|
||||
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.service.http.OkHttpUtil;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -51,13 +50,14 @@ public class PlatformWxminiConfig {
|
||||
*/
|
||||
public String jscode2session(String service, String code) {
|
||||
WxConfigBean configBean = getConfigBean(service);
|
||||
try (HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session"
|
||||
|
||||
String body = OkHttpUtil.get("https://api.weixin.qq.com/sns/jscode2session"
|
||||
+ "?appid=" + configBean.getAppId()
|
||||
+ "&secret=" + configBean.getAppSecret()
|
||||
+ "&js_code=" + code + "&grant_type=authorization_code").execute()) {
|
||||
JSONObject object = JSONUtil.parseObj(response.body());
|
||||
return object.getStr("openid");
|
||||
}
|
||||
+ "&js_code=" + code + "&grant_type=authorization_code");
|
||||
|
||||
JSONObject object = JSONUtil.parseObj(body);
|
||||
return object.getStr("openid");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,12 +67,13 @@ public class PlatformWxminiConfig {
|
||||
*/
|
||||
public String getAccessToken(String service) {
|
||||
WxConfigBean configBean = getConfigBean(service);
|
||||
try (HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token"
|
||||
|
||||
String body = OkHttpUtil.get("https://api.weixin.qq.com/cgi-bin/token"
|
||||
+ "?grant_type=client_credential&appid=" + configBean.getAppId()
|
||||
+ "&secret=" + configBean.getAppSecret()).execute()) {
|
||||
JSONObject object = JSONUtil.parseObj(response.body());
|
||||
return object.getStr("access_token");
|
||||
}
|
||||
+ "&secret=" + configBean.getAppSecret());
|
||||
|
||||
JSONObject object = JSONUtil.parseObj(body);
|
||||
return object.getStr("access_token");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ package com.tiesheng.platform.config.wxmp;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
@@ -14,6 +13,7 @@ import com.tiesheng.platform.config.wxmp.bean.WxOAuth2AccessToken;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxUserInfo;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.service.TsCacheService;
|
||||
import com.tiesheng.util.service.http.OkHttpUtil;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -69,7 +69,7 @@ public class PlatformWxmpConfig {
|
||||
query.put("grant_type", "client_credential");
|
||||
query.put("appid", configBean.getAppId());
|
||||
query.put("secret", configBean.getAppSecret());
|
||||
String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", query);
|
||||
String response = OkHttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", query);
|
||||
JSONObject respJson = JSONUtil.parseObj(response);
|
||||
accessToken = respJson.getStr("access_token");
|
||||
TsCacheService.of().put(CACHE_ACCESS_TOKEN + configBean.getAppId(), accessToken,
|
||||
@@ -91,7 +91,7 @@ public class PlatformWxmpConfig {
|
||||
Map<String, Object> query = new HashMap<>(10);
|
||||
query.put("access_token", getAccessToken(service));
|
||||
query.put("type", "jsapi");
|
||||
String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket", query);
|
||||
String response = OkHttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket", query);
|
||||
LogFactory.get().info("getJsapiTicket: " + response);
|
||||
JSONObject respJson = JSONUtil.parseObj(response);
|
||||
jsapiTicket = respJson.getStr("ticket");
|
||||
@@ -156,7 +156,7 @@ public class PlatformWxmpConfig {
|
||||
public void mediaPicGet(String service, String mediaId, String filePath) {
|
||||
String fileUrl = String.format("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s",
|
||||
getAccessToken(service), mediaId);
|
||||
HttpUtil.downloadFile(fileUrl, filePath);
|
||||
OkHttpUtil.downloadFile(fileUrl, filePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.tiesheng.platform.config.wxmp.bean;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.util.service.http.OkHttpUtil;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
@@ -13,7 +13,7 @@ public class WxOAuth2AccessToken {
|
||||
private String openid;
|
||||
|
||||
public static WxOAuth2AccessToken create(String appId, String secret, String code) {
|
||||
String response = HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token" +
|
||||
String response = OkHttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token" +
|
||||
"?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code");
|
||||
JSONObject respJson = JSONUtil.parseObj(response);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.tiesheng.platform.config.wxmp.bean;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.util.service.http.OkHttpUtil;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
@@ -30,7 +30,7 @@ public class WxUserInfo {
|
||||
* @return
|
||||
*/
|
||||
public static WxUserInfo create(WxOAuth2AccessToken oAuth2AccessToken) {
|
||||
String s = HttpUtil.get("https://api.weixin.qq.com/sns/userinfo"
|
||||
String s = OkHttpUtil.get("https://api.weixin.qq.com/sns/userinfo"
|
||||
+ "?access_token=" + oAuth2AccessToken.getAccessToken()
|
||||
+ "&openid=" + oAuth2AccessToken.getOpenid() + "&lang=zh_CN");
|
||||
return JSONUtil.toBean(s, WxUserInfo.class);
|
||||
|
||||
Reference in New Issue
Block a user