import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpsUtil {
private static final int REQUEST_SUCCESS_STATUS = 200;
protected static final Logger logger = getLogger(HttpsUtil.class);
public static final String doGet(final String url, final Map<String, Object> params) {
StringBuilder sb = new StringBuilder();
if (null != params && !params.isEmpty()) {
int i = 0;
for (String key : params.keySet()) {
if (i == 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(params.get(key));
i++;
}
}
CloseableHttpClient httpClient = createSSLClientDefault();
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(url + sb.toString());
String result = "";
try {
response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == REQUEST_SUCCESS_STATUS) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
logger.debug("{}", ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
logger.debug("{}", ex);
}
}
}
return result;
}
public static final String doPost(final String url, final Map<String, Object> params) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
if (null != params && !params.isEmpty()) {
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
nvpList.add(nvp);
}
post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
}
String result = "";
try {
response = httpClient.execute(post);
logger.debug("{}", response);
if (response.getStatusLine().getStatusCode() == REQUEST_SUCCESS_STATUS) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
logger.debug("{}", ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
logger.debug("{}", ex);
}
}
}
return result;
}
public static final String CONTENT_TYPE_TEXT_JSON = "text/json";
/**
* 发送消息
*
* @param url
* @param param
* @return
*/
public static String doPostOfJson(String url, String param) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
String result = "";
try {
post.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity se = new StringEntity(param, "utf-8");
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
post.setEntity(se);
response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == REQUEST_SUCCESS_STATUS) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
logger.debug("{}", ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
logger.debug("{}", ex);
}
}
}
return result;
public static final CloseableHttpResponse doPostResResponse(final String url, final Map<String, Object> params) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
if (null != params && !params.isEmpty()) {
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
nvpList.add(nvp);
}
post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
}
try {
response = httpClient.execute(post);
String result = "";
if (response.getStatusLine().getStatusCode() == REQUEST_SUCCESS_STATUS) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
logger.debug("{}", ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
logger.debug("{}", ex);
}
}
}
return response;
}
private static CloseableHttpClient createSSLClientDefault() {
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string) {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
logger.debug("{}", ex);
} catch (NoSuchAlgorithmException ex) {
logger.debug("{}", ex);
} catch (KeyManagementException ex) {
logger.debug("{}", ex);
}
return HttpClients.createDefault();
}
}
免证书认证 HttpClient 工具
最新推荐文章于 2024-07-21 22:27:18 发布