Java https请求工具类
2018-03-06 本文已影响0人
不敢预言的预言家
原文参照:http://blog.csdn.net/guozili1/article/details/53995121
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
/**
* https请求工具类
*
* @author seer
* @date 2018/3/6 9:12
*/
public class HttpsUtils {
/*
* https请求是在http请求的基础上加上一个ssl层
*/
public static String doPost(String requestUrl, String bodyStr, Map<String, String> header, String charset, String contentType) throws Exception {
System.out.printf("--- https post 请求地址:%s 内容:%s", requestUrl, bodyStr);
charset = null == charset ? "utf-8" : charset;
// 创建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] trustManagers = {new X509TrustManager() {
/*
* 实例化一个信任连接管理器
* 空实现是所有的连接都能访问
*/
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
// 初始化
sslContext.init(null, trustManagers, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
// 以下参照http请求
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setDoInput(true);
httpsURLConnection.setUseCaches(false);
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setRequestProperty("Accept-Charset", charset);
if (null != bodyStr) {
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(bodyStr.length()));
}
if (contentType == null) {
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
} else {
httpsURLConnection.setRequestProperty("Content-Type", contentType);
}
if (!header.isEmpty()) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpsURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
httpsURLConnection.connect();
// 读写内容
OutputStream outputStream = null;
InputStream inputStream = null;
InputStreamReader streamReader = null;
BufferedReader bufferedReader = null;
StringBuffer stringBuffer;
try {
if (null != bodyStr) {
outputStream = httpsURLConnection.getOutputStream();
outputStream.write(bodyStr.getBytes(charset));
outputStream.close();
}
if (httpsURLConnection.getResponseCode() >= 300) {
throw new Exception("https post failed, response code " + httpsURLConnection.getResponseCode());
}
inputStream = httpsURLConnection.getInputStream();
streamReader = new InputStreamReader(inputStream, charset);
bufferedReader = new BufferedReader(streamReader);
stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
} catch (Exception e) {
throw e;
} finally {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (streamReader != null) {
streamReader.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
}
System.out.printf("--- https post 返回内容:%s", stringBuffer.toString());
return stringBuffer.toString();
}
public static void main(String[] args) {
String url = args[0];
String content = args[1];
try {
HttpsUtils.doPost(url, content, null, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}