|
|
|
@ -1,26 +1,26 @@ |
|
|
|
package com.thing.sys.biz.utils; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import org.apache.http.HttpEntity; |
|
|
|
import org.apache.http.HttpResponse; |
|
|
|
import org.apache.http.NameValuePair; |
|
|
|
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.HttpClientBuilder; |
|
|
|
import org.apache.http.message.BasicNameValuePair; |
|
|
|
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
public class HttpClientUtil { |
|
|
|
public static CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|
|
|
|
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
|
|
|
/** |
|
|
|
* get请求获取String类型数据 |
|
|
|
* @param url 请求链接 |
|
|
|
@ -47,38 +47,35 @@ public class HttpClientUtil { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* post方式请求数据 |
|
|
|
* @param url 请求链接 |
|
|
|
* @param data post数据体 |
|
|
|
* @return |
|
|
|
* POST method to send JSON data |
|
|
|
* @param url Request URL |
|
|
|
* @param data Map data to be converted to JSON |
|
|
|
* @return Response as a string |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public static String post(String url, Map<String, String> data) { |
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
public static String post(String url, Map<String, Object> data) { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
List<NameValuePair> valuePairs = new ArrayList<>(); |
|
|
|
|
|
|
|
// Ensure 'data' is not null |
|
|
|
if (data != null) { |
|
|
|
// Iterate through the map entries |
|
|
|
for (Map.Entry<String, String> entry : data.entrySet()) { |
|
|
|
// Create a NameValuePair and add it to the list |
|
|
|
valuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
// Set the entity of the HttpPost with UrlEncodedFormEntity |
|
|
|
httpPost.setEntity(new UrlEncodedFormEntity(valuePairs, "UTF-8")); |
|
|
|
HttpResponse response = httpClient.execute(httpPost); |
|
|
|
// Convert Map to JSON string |
|
|
|
String jsonData = objectMapper.writeValueAsString(data); |
|
|
|
|
|
|
|
// Set the entity of the HttpPost with StringEntity containing JSON data |
|
|
|
StringEntity stringEntity = new StringEntity(jsonData, "UTF-8"); |
|
|
|
stringEntity.setContentType("application/json"); |
|
|
|
httpPost.setEntity(stringEntity); |
|
|
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
|
|
|
HttpEntity httpEntity = response.getEntity(); |
|
|
|
BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); |
|
|
|
try (BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent())) { |
|
|
|
byte[] buffer = new byte[128]; |
|
|
|
int bytesRead; |
|
|
|
// Read the response content |
|
|
|
while ((bytesRead = bis.read(buffer)) != -1) { |
|
|
|
sb.append(new String(buffer, 0, bytesRead, "UTF-8")); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
// Handle encoding errors |
|
|
|
e.printStackTrace(); |
|
|
|
|