RemoteHttpUtil

  1 package com.-.it.regioc.utils;
  2 
  3 import com.-.it.regioc.bean.config.HttpConfig;
  4 import okhttp3.*;
  5 import org.apache.commons.lang3.StringUtils;
  6 import org.slf4j.Logger;
  7 import org.slf4j.LoggerFactory;
  8 import org.springframework.beans.factory.annotation.Value;
  9 import org.springframework.stereotype.Component;
 10 
 11 import javax.annotation.Resource;
 12 import javax.ws.rs.core.HttpHeaders;
 13 import java.io.IOException;
 14 import java.util.Map;
 15 import java.util.concurrent.TimeUnit;
 16 
 17 @Component
 18 public class RemoteHttpUtil {
 19 
 20     public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
 21 
 22     private static final Logger LOGGER = LoggerFactory.getLogger(RemoteHttpUtil.class);
 23 
 24     @Value("${request.application.appId}")
 25     private String appId;
 26 
 27     @Value("${request.appSecret}")
 28     private String appSecret;
 29 
 30     @Resource
 31     private HttpConfig httpConfig;
 32 
 33     @Resource
 34     private DynamicTokenGet dynamicTokenGet;
 35 
 36     private OkHttpClient client = new OkHttpClient().newBuilder()
 37             .connectTimeout(60, TimeUnit.SECONDS)
 38             .readTimeout(60, TimeUnit.SECONDS)
 39             .build();
 40     ;
 41 
 42     public String get(String restUrl) throws IOException {
 43         Request request = createRequest(restUrl).build();
 44         try (Response response = client.newCall(request).execute()) {
 45             if (!response.isSuccessful()) {
 46                 throw new IOException("Unexpected code " + response);
 47             }
 48             return response.body().string();
 49         }
 50     }
 51 
 52     public String get(final String restUrl, String parameters) throws IOException {
 53         if (StringUtils.isNotEmpty(parameters)) {
 54             return get(restUrl + "?" + parameters);
 55         }
 56         return get(restUrl);
 57     }
 58 
 59     public String post(String restUrl, String json) {
 60         RequestBody body = RequestBody.create(JSON, json);
 61         Request.Builder requestBuilder = createRequest(restUrl);
 62         Request request = requestBuilder.post(body).build();
 63         try (Response response = client.newCall(request).execute()) {
 64             return response.body().string();
 65         } catch (Exception e) {
 66             LOGGER.error("post error:", e);
 67         }
 68         return "";
 69     }
 70 
 71     public String post(String restUrl, String json, Map<String, String> headers) {
 72         RequestBody body = RequestBody.create(JSON, json);
 73         Request.Builder requestBuilder = createRequest(restUrl);
 74         for (Map.Entry<String, String> entry : headers.entrySet()) {
 75             requestBuilder.addHeader(entry.getKey(), entry.getValue());
 76         }
 77         Request request = requestBuilder.post(body).build();
 78         try (Response response = client.newCall(request).execute()) {
 79             return response.body().string();
 80         } catch (Exception e) {
 81             LOGGER.error("post error:", e);
 82         }
 83         return "";
 84     }
 85 
 86     public String put(String restUrl, String json) {
 87         RequestBody body = RequestBody.create(JSON, json);
 88         Request.Builder requestBuilder = createRequest(restUrl);
 89         Request request = requestBuilder.put(body).build();
 90         try (Response response = client.newCall(request).execute()) {
 91             return response.body().string();
 92         } catch (Exception e) {
 93             LOGGER.error("put error:", e);
 94         }
 95         return "";
 96     }
 97 
 98     private Request.Builder createRequest(String url) {
 99         //获取动态token
100         return new Request.Builder().url(url)
101                 .header("Content-Type", "application/json;charset=UTF-8")
102                 .addHeader(HttpHeaders.AUTHORIZATION, dynamicTokenGet.getDynamicToken());
103     }
104 }

 

posted @ 2022-11-14 23:11  MING5135  阅读(15)  评论(0编辑  收藏  举报