1. 核心代码
HttpsPost.java
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpsPost {
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
/**
* post方式请求服务器(https协议)
*
* @param url
* 请求地址
* @param content
* 参数
* @param charset
* 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public ByteArrayOutputStream post(String url, String content, String charset)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
HttpsURLConnection conn = sendPost(url, content, charset);
InputStream is = conn.getInputStream();
try {
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
return outStream;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
return null;
}
public void post(String url, String content, String charset, String filePath)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
HttpsURLConnection conn = sendPost(url, content, charset);
InputStream is = null;
FileOutputStream fs = null;
try {
is = conn.getInputStream();
String filename = conn.getHeaderField("Content-Disposition");
String regex = ".+filename=\"(.+?)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(filename);
if (matcher.matches()) {
if (is != null) {
fs = new FileOutputStream(new File(filePath
+ matcher.group(1)));
byte[] buffer = new byte[2048];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fs.write(buffer, 0, len);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
if (fs != null) {
fs.close();
}
}
}
public HttpsURLConnection sendPost(String url, String content,
String charset) throws NoSuchAlgorithmException,
KeyManagementException, IOException {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(content.getBytes(charset));
// 刷新、关闭
out.flush();
out.close();
return conn;
}
public String getResult(String url) {
String result = "";
try {
HttpsURLConnection conn = sendPost(url, "", "UTF-8");
InputStream in = conn.getInputStream();
if (in != null) {
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
result += line;
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
result = new String(result.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
}
2. 使用方法
HttpsPost httpsPost = new HttpsPost();
String result = httpsPost.getResult("https://ip:port/publicKey?userid=" + MD5Util.string2MD5("test"));
System.out.println(result);
返回结果:
{"success":true,"msg":"626002365e48146a0063b7cb1b7f470e"}