转发之前,先发表两句感慨。https访问好麻烦,跟http还是有很大区别的。另外在weblogic、tomcat、jboss上用https也是有差异的,和容器有关。
代码如下:
- public static JSONObjects httpsRequest(String requestUrl, String requestMethod, String outputStr) {
- JSONObject jsonObject = null;
- try {
-
- TrustManager[] tm = { new MyX509TrustManager() };
- SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
- sslContext.init(null, tm, new java.security.SecureRandom());
-
- SSLSocketFactory ssf = sslContext.getSocketFactory();
- log.info("Common=========== ");
- URL url = new URL(requestUrl);
- log.info("Common=========== ");
- HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
- conn.setSSLSocketFactory(ssf);