日期:2014-05-17 浏览次数:21006 次
public String postHttps(String urlString, String data, Logger logger) throws Exception { SSLContext sc = null; TrustManager[] trustAllCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; try { sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { } HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return urlHostName.equals(session.getPeerHost()); } }; HttpsURLConnection.setDefaultHostnameVerifier(hv); OutputStreamWriter os = null; BufferedReader reader = null; StringBuffer sb = new StringBuffer(); URL u = new URL(urlString); HttpsURLConnection conn = (HttpsURLConnection) u.openConnection(); // conn.setRequestProperty("Content-Type", "text/xml"); conn.setDoOutput(true); conn.setFollowRedirects(true); // conn.setReadTimeout(30000); os = new OutputStreamWriter(conn.getOutputStream()); os.write(data); os.flush(); os.close(); return null; }
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ page import="java.net.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>测试Https</title> </head> <body> <% out.print("默认:<br/>"); URL aURL = new URL("https://www.verisign.com"); URLConnection urlConn = aURL.openConnection(); if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) { out.print("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection<br/>"); } if (urlConn instanceof javax.net.ssl.HttpsURLConnection) { out.print("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection<br/>"); } if (urlConn instanceof HttpURLConnection) { out.print("*** openConnection returns an instnace of HttpURLConnection<br/>"); } %> </body> </html>