import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpsReader {
public String getHTML(String url) throws IOException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new 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) {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
URL sslUrl = new url(/blog/url);
URLConnection inConnection = sslUrl.openConnection();
InputStream is = inConnection.getInputStream();
byte[] buffer = new byte[16384];
StringBuffer sb = new StringBuffer();
while (true) {
int bytesRead = is.read(buffer, 0, buffer.length);
if (bytesRead < 0)
break;
String s = new String(buffer, 0, bytesRead, "GB2312");
sb.append(s);
}
is.close();
String outContent = sb.toString();
return outContent;
}
}
没有评论:
发表评论