HttpUtil.java
package com.dhc.task.wx.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
public class HttpUtil {
/**
* 获取一个http/https连接
* @param urlStr
* @param method
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws IOException
* @throws KeyManagementException
*/
private static HttpsURLConnection getHttpConnection(String urlStr, String method) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
URL url = new URL(urlStr);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(method);
return httpUrlConn;
}
/**
* http/https get请求
* @param urlStr
* @return
* @throws Exception
*/
public static String get(String urlStr) throws Exception {
HttpsURLConnection httpUrlConn = getHttpConnection(urlStr, "GET");
// 取得输入流
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 读取响应内容
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
httpUrlConn.disconnect();
return buffer.toString();
}
/**
* http/https post请求 返回响应结果
* @param urlStr
* @param body
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws KeyManagementException
*/
public static String Post(String urlStr, String body) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
HttpsURLConnection httpUrlConn = getHttpConnection(urlStr, "POST");;
httpUrlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpUrlConn.connect();
OutputStream os = httpUrlConn.getOutputStream();
os.write(body.getBytes("UTF-8"));
os.close();
// 取得输入流
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 读取响应内容
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
httpUrlConn.disconnect();
return buffer.toString();
}
}
MyX509TrustManager.java
package com.dhc.task.wx.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* 类名: MyX509TrustManager </br>
* 描述: 信任管理器 </br>
*/
public class MyX509TrustManager implements X509TrustManager {
// 检查客户端证书
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 检查服务器端证书
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 返回受信任的X509证书数组
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
注:参考网上博客+个人修改 可以发送https http的get post请求,接收到返回的body