大概介绍:php语言写的接口,通过get或post方式获取里面的数据。但是是以https开头的URL
原因:https网址向客户的要证书
解决方案:跳过证书验证部分
package com.scheduler; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; //该类屏蔽了https连接需要加密证书的验证 public class GetTextByUrl { public static void main(String[] args) { HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost()); return true; } }; StringBuffer msg = new StringBuffer(); try { URL url = new URL("https://www.ddsdd.php"); trustAllHttpsCertificates(); HttpsURLConnection.setDefaultHostnameVerifier(hv); URLConnection connection = url.openConnection(); connection.connect(); BufferedReader in = new BufferedReader((new InputStreamReader(connection.getInputStream()))); String line; while ((line = in.readLine()) != null) { msg.append(line); } String msgString = msg.toString(); String fannlyMsg = msgString.substring(msgString.indexOf("!") + 1); System.out.println(msgString); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private static void trustAllHttpsCertificates() throws Exception { javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1]; javax.net.ssl.TrustManager tm = new miTM(); trustAllCerts[0] = tm; javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext .getInstance("SSL"); sc.init(null, trustAllCerts, null); javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc .getSocketFactory()); } static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public boolean isServerTrusted( java.security.cert.X509Certificate[] certs) { return true; } public boolean isClientTrusted( java.security.cert.X509Certificate[] certs) { return true; } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } } }