|
@@ -0,0 +1,196 @@
|
|
|
+package com.zjugis.auth.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.serializer.ValueFilter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+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 java.io.BufferedReader;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author ljy
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2022/5/7 15:36
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class RequestUtils {
|
|
|
+
|
|
|
+ public static String get(String urlStr,String params) {
|
|
|
+
|
|
|
+ String responseMsg = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 访问地址url对象
|
|
|
+ URL url = new URL(urlStr +"?"+ params);
|
|
|
+ // 创建网络连接对象 http
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置请求方式
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ // 设置超时时间 3秒
|
|
|
+ connection.setConnectTimeout(3000);
|
|
|
+ // 设置是否自行http重定向
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ // 设置是否使用缓存
|
|
|
+ connection.setDefaultUseCaches(true);
|
|
|
+ // 设置是否从HttpURLConnection获取输出
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ // 设置是否从HttpURLConnection输入
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // 创建连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取响应码
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+
|
|
|
+ // 判断是否成功响应
|
|
|
+ if (responseCode == 200) {
|
|
|
+ // 从响应流中读取数据
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line = "";
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ responseMsg += line;
|
|
|
+ }
|
|
|
+ // 关闭数据流
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+ // 断开连接
|
|
|
+ connection.disconnect();
|
|
|
+
|
|
|
+ return responseMsg;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return responseMsg;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String httpURLConnectionPOST(String urlStr, Map<String, Object> map) throws Exception {
|
|
|
+ HttpURLConnection connection = getHttpURLConnection(urlStr, map);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String str = null;
|
|
|
+ while ((str = in.readLine()) != null) {
|
|
|
+ sb.append(str);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new Exception("请求返回:" + connection.getResponseCode());
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, Object> httpURLConnectionPOSTJson(String urlStr, Object obj) throws Exception {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ final HttpPost httpPost = new HttpPost(urlStr);
|
|
|
+ //设置请求体参数
|
|
|
+ StringEntity entity = new StringEntity(JSONObject.toJSONString(obj),"UTF-8");
|
|
|
+ entity.setContentEncoding("utf-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ //设置请求头部
|
|
|
+ httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ //执行请求,返回请求响应
|
|
|
+ try (final CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ //请求返回状态码
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ //请求成功
|
|
|
+ if (statusCode == HttpStatus.SC_OK && statusCode <= HttpStatus.SC_TEMPORARY_REDIRECT) {
|
|
|
+ //取出响应体
|
|
|
+ final HttpEntity entity2 = response.getEntity();
|
|
|
+ //从响应体中解析出token
|
|
|
+ String responseBody = EntityUtils.toString(entity2, "utf-8");
|
|
|
+ jsonObject = JSONObject.parseObject(responseBody);
|
|
|
+ //token = jsonObject.getString("access_token");
|
|
|
+ } else {
|
|
|
+ //请求失败
|
|
|
+ throw new ClientProtocolException("请求失败,响应码为:" + statusCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static HttpURLConnection getHttpURLConnectionJson(String urlStr, Map<String, Object> map) throws IOException {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json");
|
|
|
+ connection.connect();
|
|
|
+ try (DataOutputStream dataout = new DataOutputStream(connection.getOutputStream())) {
|
|
|
+ StringBuilder params = new StringBuilder();
|
|
|
+ if (null != map) {
|
|
|
+ for (String keymap : map.keySet()) {
|
|
|
+ if (null != map.get(keymap)) {
|
|
|
+ params.append(keymap).append("=");
|
|
|
+ if (map.get(keymap) instanceof String) {
|
|
|
+ params.append(URLEncoder.encode(String.valueOf(map.get(keymap)), "utf-8")).append("&");
|
|
|
+ } else {
|
|
|
+ params.append(URLEncoder.encode(JSON.toJSONString(map.get(keymap), (ValueFilter) (o, s, value) -> value == null ? "" : value), "utf-8")).append("&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (params.length() > 0) {
|
|
|
+ dataout.writeBytes(params.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return connection;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static HttpURLConnection getHttpURLConnection(String urlStr, Map<String, Object> map) throws IOException {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.connect();
|
|
|
+ try (DataOutputStream dataout = new DataOutputStream(connection.getOutputStream())) {
|
|
|
+ StringBuilder params = new StringBuilder();
|
|
|
+ if (null != map) {
|
|
|
+ for (String keymap : map.keySet()) {
|
|
|
+ if (null != map.get(keymap)) {
|
|
|
+ params.append(keymap).append("=");
|
|
|
+ if (map.get(keymap) instanceof String) {
|
|
|
+ params.append(URLEncoder.encode(String.valueOf(map.get(keymap)), "utf-8")).append("&");
|
|
|
+ } else {
|
|
|
+ params.append(URLEncoder.encode(JSON.toJSONString(map.get(keymap), (ValueFilter) (o, s, value) -> value == null ? "" : value), "utf-8")).append("&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (params.length() > 0) {
|
|
|
+ dataout.writeBytes(params.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return connection;
|
|
|
+ }
|
|
|
+}
|