一、发送请求http请求
package com.ykq;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class SendHttpUtil {
public static String sendHttp(String method, String urlStr,
String paramStr, Map<String, String> requestProperty) {
OutputStream os = null;
InputStream is = null;
BufferedInputStream bis = null;
HttpURLConnection httpConnection = null;
try {
httpConnection = getHttpConnect(httpConnection, urlStr,
method, requestProperty);
httpConnection.connect();
String respStr = getRespStr(httpConnection, paramStr,
os, is, bis);
return respStr;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeStreamAndConnect(os, is, bis, httpConnection);
}
return "";
}
private static HttpURLConnection getHttpConnect(
HttpURLConnection httpConnection, String urlStr,
String method, Map<String, String> requestProperty)
throws Exception {
try {
URL url = new URL(urlStr);
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(3000);
httpConnection.setReadTimeout(3000);
httpConnection.setRequestMethod(method);
httpConnection.setUseCaches(false);
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
if(requestProperty != null && !requestProperty.isEmpty()) {
Set<Entry<String, String>> propertyEntry
= requestProperty.entrySet();
Iterator<Entry<String, String>> iterator
= propertyEntry.iterator();
Entry<String, String> entry = null;
while (iterator.hasNext()) {
entry = iterator.next();
httpConnection.setRequestProperty(entry.getKey(),
entry.getValue());
}
}
} catch (Exception e) {
throw e;
}
return httpConnection;
}
private static String getRespStr(HttpURLConnection httpConnection,
String paramStr, OutputStream os, InputStream is,
BufferedInputStream bis) throws Exception {
StringBuffer respBuffer = new StringBuffer();
try {
if (paramStr != null && !paramStr.isEmpty()) {
os = httpConnection.getOutputStream();
byte[] b = paramStr.getBytes("UTF-8");
os.write(b, 0, b.length);
}
if (HttpURLConnection.HTTP_OK
== httpConnection.getResponseCode()) {
is = httpConnection.getInputStream();
bis = new BufferedInputStream(is);
byte[] respByte = new byte[1024];
while(bis.read(respByte) > 0) {
respBuffer.append(new String(respByte, "UTF-8"));
}
}
} catch(Exception e) {
throw e;
}
return respBuffer.toString();
}
private static void closeStreamAndConnect(OutputStream os,
InputStream is, BufferedInputStream bis,
HttpURLConnection connection) {
try {
connection.disconnect();
} catch(Exception e) {
}
try {
if (is != null) {
is.close();
}
} catch(Exception e) {
}
try {
if (bis != null) {
bis.close();
}
} catch(Exception e) {
}
try {
if (os != null) {
os.close();
}
} catch(Exception e) {
}
}
}
二、发送https请求——信任所有证书
package com.ykq;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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 SendHttpsUtil {
private static final HostnameVerifier DO_NOT_VERIFY
= new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return;
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return;
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
public static String sendHttps(String method, String urlStr,
String paramStr, Map<String, String> requestProperty) {
OutputStream os = null;
InputStream is = null;
BufferedInputStream bis = null;
HttpsURLConnection httpsConnection = null;
try {
trustAllHttpsCertificates();
httpsConnection = getHttpConnect(httpsConnection, urlStr,
method, requestProperty);
httpsConnection.connect();
String respStr = getRespStr(httpsConnection, paramStr,
os, is, bis);
return respStr;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeStreamAndConnect(os, is, bis, httpsConnection);
}
return "";
}
private static HttpsURLConnection getHttpConnect(
HttpsURLConnection httpsConnection, String urlStr, String method,
Map<String, String> requestProperty) throws Exception {
try {
URL url = new URL(urlStr);
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.setHostnameVerifier(DO_NOT_VERIFY);
httpsConnection.setConnectTimeout(3000);
httpsConnection.setReadTimeout(3000);
httpsConnection.setRequestMethod(method);
httpsConnection.setUseCaches(false);
httpsConnection.setDoInput(true);
httpsConnection.setDoOutput(true);
if(requestProperty != null && !requestProperty.isEmpty()) {
Set<Entry<String, String>> propertyEntry
= requestProperty.entrySet();
Iterator<Entry<String, String>> iterator
= propertyEntry.iterator();
Entry<String, String> entry = null;
while (iterator.hasNext()) {
entry = iterator.next();
httpsConnection.setRequestProperty(entry.getKey(),
entry.getValue());
}
}
} catch (Exception e) {
throw e;
}
return httpsConnection;
}
private static String getRespStr(
HttpsURLConnection httpsConnection, String paramStr,
OutputStream os, InputStream is, BufferedInputStream bis)
throws Exception {
StringBuffer respBuffer = new StringBuffer();
try {
if (paramStr != null && !paramStr.isEmpty()) {
os = httpsConnection.getOutputStream();
byte[] b = paramStr.getBytes("UTF-8");
os.write(b, 0, b.length);
}
if (HttpsURLConnection.HTTP_OK
== httpsConnection.getResponseCode()) {
is = httpsConnection.getInputStream();
bis = new BufferedInputStream(is);
byte[] respByte = new byte[1024];
while(bis.read(respByte) > 0) {
respBuffer.append(new String(respByte, "UTF-8"));
}
}
} catch(Exception e) {
throw e;
}
return respBuffer.toString();
}
private static void closeStreamAndConnect(OutputStream os,
InputStream is, BufferedInputStream bis,
HttpsURLConnection connection) {
try {
connection.disconnect();
} catch(Exception e) {
}
try {
if (is != null) {
is.close();
}
} catch(Exception e) {
}
try {
if (bis != null) {
bis.close();
}
} catch(Exception e) {
}
try {
if (os != null) {
os.close();
}
} catch(Exception e) {
}
}
}
三、发送https请求——校验服务端证书
package com.ykq;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class SendHttpsUtil {
private static void trustAllHttpsCertificates(String cAalias,
String certificate) throws Exception {
CertificateFactory certificateFactory =
CertificateFactory.getInstance("X.509");
KeyStore keyStore =
KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
FileInputStream caInputStream
= new FileInputStream(new File(certificate));
keyStore.setCertificateEntry(cAalias,
certificateFactory.generateCertificate(caInputStream));
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers =
trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 ||
!(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager x509TrustManager = (X509TrustManager) trustManagers[0];
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null,
new TrustManager[]{x509TrustManager},
SecureRandom.getInstanceStrong());
HttpsURLConnection.setDefaultSSLSocketFactory(
sslContext.getSocketFactory());
}
public static String sendHttps(String method, String urlStr, String paramStr,
Map<String, String> requestProperty, String cAalias, String certificate) {
OutputStream os = null;
InputStream is = null;
BufferedInputStream bis = null;
HttpsURLConnection httpsConnection = null;
try {
trustAllHttpsCertificates(cAalias, certificate);
httpsConnection = getHttpConnect(httpsConnection, urlStr, method, requestProperty);
httpsConnection.connect();
String respStr = getRespStr(httpsConnection, paramStr, os, is, bis);
return respStr;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeStreamAndConnect(os, is, bis, httpsConnection);
}
return "";
}
private static HttpsURLConnection getHttpConnect(
HttpsURLConnection httpsConnection, String urlStr,
String method, Map<String, String> requestProperty)
throws Exception {
try {
URL url = new URL(urlStr);
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.setConnectTimeout(3000);
httpsConnection.setReadTimeout(3000);
httpsConnection.setRequestMethod(method);
httpsConnection.setUseCaches(false);
httpsConnection.setDoInput(true);
httpsConnection.setDoOutput(true);
if(requestProperty != null && !requestProperty.isEmpty()) {
Set<Entry<String, String>> propertyEntry
= requestProperty.entrySet();
Iterator<Entry<String, String>> iterator
= propertyEntry.iterator();
Entry<String, String> entry = null;
while (iterator.hasNext()) {
entry = iterator.next();
httpsConnection.setRequestProperty(entry.getKey(),
entry.getValue());
}
}
} catch (Exception e) {
throw e;
}
return httpsConnection;
}
private static String getRespStr(HttpsURLConnection httpsConnection,
String paramStr, OutputStream os, InputStream is,
BufferedInputStream bis) throws Exception {
StringBuffer respBuffer = new StringBuffer();
try {
if (paramStr != null && !paramStr.isEmpty()) {
os = httpsConnection.getOutputStream();
byte[] b = paramStr.getBytes("UTF-8");
os.write(b, 0, b.length);
}
if (HttpsURLConnection.HTTP_OK
== httpsConnection.getResponseCode()) {
is = httpsConnection.getInputStream();
bis = new BufferedInputStream(is);
byte[] respByte = new byte[1024];
while(bis.read(respByte) > 0) {
respBuffer.append(new String(respByte, "UTF-8"));
}
}
} catch(Exception e) {
throw e;
}
return respBuffer.toString();
}
private static void closeStreamAndConnect(OutputStream os,
InputStream is, BufferedInputStream bis,
HttpsURLConnection connection) {
try {
connection.disconnect();
} catch(Exception e) {
}
try {
if (is != null) {
is.close();
}
} catch(Exception e) {
}
try {
if (bis != null) {
bis.close();
}
} catch(Exception e) {
}
try {
if (os != null) {
os.close();
}
} catch(Exception e) {
}
}
}