|
@@ -11,6 +11,7 @@ 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.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
@@ -172,34 +173,22 @@ public class HttpClientThreeUtil {
|
|
|
*/
|
|
|
public static String post(String url, String data) {
|
|
|
String responseBody = "";
|
|
|
-
|
|
|
- //指定url,和http方式
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setConfig(HUIRONG_CONFIG);
|
|
|
- //设置类型
|
|
|
- StringEntity se = new StringEntity(data, "utf-8");
|
|
|
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
+
|
|
|
+ // 使用 ContentType.APPLICATION_JSON 构造请求实体,确保与 Postman 一致
|
|
|
+ StringEntity se = new StringEntity(data, ContentType.APPLICATION_JSON);
|
|
|
httpPost.setEntity(se);
|
|
|
- se.setContentType("application/json");
|
|
|
- //HttpEntity
|
|
|
- HttpEntity entity = null;
|
|
|
- //请求数据
|
|
|
+
|
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
|
response = httpClient.execute(httpPost);
|
|
|
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
- entity = response.getEntity();
|
|
|
- responseBody = EntityUtils.toString(entity);
|
|
|
- }else{
|
|
|
- throw new ServiceException("接口调用异常:" + response.getStatusLine().getStatusCode() + " " + url);
|
|
|
- }
|
|
|
+ // 直接获取实体内容,由外层处理状态码问题
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ responseBody = EntityUtils.toString(entity, "UTF-8");
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
} finally {
|
|
|
- // 关闭连接,释放资源
|
|
|
- if (entity != null) {
|
|
|
- EntityUtils.consumeQuietly(entity); //会自动释放连接
|
|
|
- }
|
|
|
if (response != null) {
|
|
|
try {
|
|
|
response.close();
|
|
@@ -211,6 +200,7 @@ public class HttpClientThreeUtil {
|
|
|
return responseBody;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public static String post(String url, String data,String mchId) {
|
|
|
String responseBody = "";
|
|
|
|