package com.qianwen.smartman.modules.dnc.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.net.URLDecoder;
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.Map;
|
import java.util.Objects;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
import com.qianwen.smartman.common.constant.DncConstant;
|
|
public class HttpClientUtils {
|
public static JSONObject postForForm(String url, Map<String, String> parms) {
|
HttpPost httpPost = new HttpPost(url);
|
ArrayList<BasicNameValuePair> list = new ArrayList<>();
|
parms.forEach((key, value) -> {
|
list.add(new BasicNameValuePair(key, value));
|
});
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
try {
|
try {
|
if (parms.size() > 0) {
|
httpPost.setEntity(new UrlEncodedFormEntity(list, DncConstant.CHARSET));
|
}
|
InputStream content = httpPost.getEntity().getContent();
|
InputStreamReader inputStreamReader = new InputStreamReader(content, StandardCharsets.UTF_8);
|
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
String readLine = bufferedReader.readLine();
|
String s = URLDecoder.decode(readLine, DncConstant.CHARSET);
|
System.out.println("readLine===================================" + readLine);
|
System.out.println("s==========================================" + s);
|
HttpEntity entity = httpClient.execute(httpPost).getEntity();
|
JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(entity, DncConstant.CHARSET));
|
if (Objects.nonNull(httpClient)) {
|
try {
|
httpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return jsonObject;
|
} catch (IOException e2) {
|
e2.printStackTrace();
|
if (Objects.nonNull(httpClient)) {
|
try {
|
httpClient.close();
|
return null;
|
} catch (IOException e3) {
|
e3.printStackTrace();
|
return null;
|
}
|
}
|
return null;
|
}
|
} catch (Throwable th) {
|
if (Objects.nonNull(httpClient)) {
|
try {
|
httpClient.close();
|
} catch (IOException e4) {
|
e4.printStackTrace();
|
}
|
}
|
throw th;
|
}
|
}
|
}
|