|
|
|
@ -1,17 +1,28 @@
|
|
|
|
|
package com.sztzjy.forex.trading_trading.util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import com.nimbusds.jose.shaded.gson.JsonObject;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class HttpUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
|
|
|
|
|
|
|
|
|
|
// 发送GET请求
|
|
|
|
|
public static String sendGet(String url) throws IOException {
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
@ -41,41 +52,46 @@ public class HttpUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
public static String sendPost(String url, String requestBody) throws IOException {
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
BufferedReader reader = null;
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
public static JSONObject sendPost(String url, String paramStr, String contentType, String token) throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
URL requestUrl = new URL(url);
|
|
|
|
|
connection = (HttpURLConnection) requestUrl.openConnection();
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
|
|
|
if(statusCode != HttpURLConnection.HTTP_OK){
|
|
|
|
|
log.error("请求失败,返回码:" + statusCode + ",返回消息:" + connection.getResponseMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
JSONObject ret = new JSONObject();
|
|
|
|
|
HttpPost method = new HttpPost(url);
|
|
|
|
|
StringEntity entity = new StringEntity(paramStr, "utf-8");
|
|
|
|
|
entity.setContentEncoding("UTF-8");
|
|
|
|
|
|
|
|
|
|
OutputStream outputStream = connection.getOutputStream();
|
|
|
|
|
outputStream.write(requestBody.getBytes());
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
if (StringUtils.isEmpty(contentType))
|
|
|
|
|
entity.setContentType("application/json");
|
|
|
|
|
else
|
|
|
|
|
entity.setContentType(contentType);
|
|
|
|
|
|
|
|
|
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
response.append(line);
|
|
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
|
|
method.setHeader("Authorization", "Bearer " + token);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (reader != null) {
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
|
|
method.setEntity(entity);
|
|
|
|
|
int timeout = 60000;
|
|
|
|
|
RequestConfig requestConfig = RequestConfig.custom()
|
|
|
|
|
.setSocketTimeout(timeout)
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.build();
|
|
|
|
|
method.setConfig(requestConfig);
|
|
|
|
|
HttpClient client = HttpClients.createDefault();
|
|
|
|
|
HttpResponse resp = client.execute(method);
|
|
|
|
|
int statusCode = resp.getStatusLine().getStatusCode();
|
|
|
|
|
if (statusCode != HttpStatus.SC_OK) {
|
|
|
|
|
log.info("接口请求失败,返回码:" + statusCode + ",失败原因:" + resp.getStatusLine().getReasonPhrase());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
String respString = EntityUtils.toString(resp.getEntity(), "UTF-8");
|
|
|
|
|
ret.set("statusCode",statusCode);
|
|
|
|
|
if(StringUtils.isNotEmpty(respString)){
|
|
|
|
|
ret.set("respString",respString);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
log.info("接口请求失败,失败原因:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.toString();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|