import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* https請求工具類
*/
public class HttpsUtil {
/**
* https get请求
* @param url
* @return
*/
public static String httpsGet(String url) {
String str_return = "";
HttpsURLConnection conn = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, ("ISO-8859-1")));
String ret = "";
while (ret != null) {
ret = br.readLine();
if (ret != null && !ret.trim().equals("")) {
str_return = str_return + new String(ret.getBytes("ISO-8859-1"), "utf-8");
}
}
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return str_return;
}
/**
* https Post请求
* @param url
* @return
*/
public static String httpsPost(String url, Map parameters) {
String str_return = "";
HttpsURLConnection conn = null;
try {
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext(); ) {
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(), "utf-8"));
params.append("&");
}
if (params.length() > 0) {
params = params.deleteCharAt(params.length() - 1);
}
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
conn = (HttpsURLConnection) console.openConnection();
conn.setRequestMethod("POST");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true);
byte[] b = params.toString().getBytes();
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, ("ISO-8859-1")));
String ret = "";
while (ret != null) {
ret = br.readLine();
if (ret != null && !ret.trim().equals("")) {
str_return = str_return + new String(ret.getBytes("ISO-8859-1"), "utf-8");
}
}
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return str_return;
}
public static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}
public static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
}
https請求工具類
最新推荐文章于 2024-10-30 20:54:59 发布
该代码示例展示了如何在Java中创建一个HTTPS请求工具类,包括GET和POST方法。它使用自定义的信任管理器和主机名验证器,允许连接到任何服务器而忽略证书错误。此方法适用于测试环境,但在生产环境中应谨慎处理安全问题。
摘要由CSDN通过智能技术生成