|
|
@@ -0,0 +1,54 @@
|
|
|
+package com.zksy.data.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.zksy.data.config.XhConfigProperty;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.HttpUrl;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class XhRequestUtil {
|
|
|
+ @Autowired
|
|
|
+ private XhConfigProperty xhConfigProperty;
|
|
|
+ @Autowired
|
|
|
+ private OkHttpClient okHttpClient;
|
|
|
+ @Autowired
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+ public String httpRequestByUniCode(String uniCode,String address){
|
|
|
+ HttpUrl url = new HttpUrl.Builder()
|
|
|
+ .scheme("http")
|
|
|
+ .host(xhConfigProperty.getHost())
|
|
|
+ .port(Integer.valueOf(xhConfigProperty.getPort()))
|
|
|
+ .addPathSegment(address)
|
|
|
+ .addQueryParameter("UNISCID", uniCode)
|
|
|
+ .build();
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .addHeader("appKey", xhConfigProperty.getAppKey())
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ Response response = okHttpClient.newCall(request).execute();
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ log.error("当前接口请求失败:{}",address);
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isEmpty(response.body())){
|
|
|
+ log.error("当前接口请求数据出现问题:{}",address);
|
|
|
+ }
|
|
|
+ String s = response.body().string();
|
|
|
+// objectMapper.readValue(s,String.class);
|
|
|
+ log.info("当前的请求结果:{}",s);
|
|
|
+ return s;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|