|
|
@@ -0,0 +1,64 @@
|
|
|
+package cn.seecoder.seecoderportalserver.service;
|
|
|
+
|
|
|
+import cn.seecoder.seecoderportalserver.exception.ExceptionType;
|
|
|
+import cn.seecoder.seecoderportalserver.exception.PortalException;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.http.Consts;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class HttpService {
|
|
|
+
|
|
|
+ public String sendPost(String uri, JSONObject requestBody, String token) throws Exception {
|
|
|
+ StringEntity requestEntity = new StringEntity(JSON.toJSONString(requestBody));
|
|
|
+ HttpPost httpPost = new HttpPost(uri);
|
|
|
+ httpPost.setHeader("Authorization", token);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json;chartset=UTF-8");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+ httpPost.setConfig(RequestConfig.DEFAULT);
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ client.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendGet(String uri, List<NameValuePair> params, String token) throws Exception {
|
|
|
+ String requestParam = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
|
|
|
+ HttpGet httpGet = new HttpGet(uri + "?" + requestParam);
|
|
|
+ httpGet.setHeader("Authorization", token);
|
|
|
+ httpGet.setConfig(RequestConfig.DEFAULT);
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ CloseableHttpResponse response = client.execute(httpGet);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ client.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getToken() {
|
|
|
+ HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
|
|
+ String token = Optional.ofNullable(httpServletRequest.getHeader("Authorization")).orElseThrow(() -> new PortalException(ExceptionType.UNAUTHORIZED, "缺少Token"));
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+}
|