package com.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.GeneralSecurityException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
/**
* @author zhang-long
* 读取URL页面中的内容,支持HTTP和HTTPS协议
*/
public class ReadURLUtil {
// X509TrustManager接口的实例管理使用哪一个 X509 证书来验证远端的安全套接字。决定是根据信任的证
//书授权、证书撤消列表、在线状态检查或其他方式做出的。
private LmX509TrustManager xtm = new LmX509TrustManager();
//HostnameVerifier类是用于主机名验证的基接口
private LmHostnameVerifier hnv = new LmHostnameVerifier();
/**
* 初始化context及connection
*/
private void initContext() {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL", "SunJSSE");
X509TrustManager[] xtmArray = new X509TrustManager[]{xtm};
sslContext.init(null, xtmArray, new java.security.SecureRandom());
System.out.println("初始化正常!");
} catch (GeneralSecurityException gse) {
System.out.println("初始化SSL异常!" + gse);
}
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}
// 以charSet为指定的编码格式,读取给出的urlStr的地址中的XML文件字符串
public String getString(String urlStr, String charSet) {
// String urlStr =
// "http://localhost:8080/SAP/eventTypeBusiness.do?method=query¤tPage=1&pageSize=10";
String errorMsg = "";
// 没有设置字符集的时候,使用UTF-8的字符
if (charSet == null || "".equals(charSet)) {
charSet = "UTF-8";
}
try {
URL url = new URL(urlStr);
if ("https".equals(url.getProtocol())) {
// 初始化证书,信任HTTPS证书
initContext();
}
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(connection.getInputStream(), charSet));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
buffer.append(line + "\n");
}
bufferedReader.close();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return errorMsg;
}
}
public static void main(String[] args) {
String url = "https://login.taobao.com/member/login.jhtml";
//String url = "http://www.baidu.com";
System.out.println(url);
String charset = "gbk";
ReadURLUtil util = new ReadURLUtil();
String result = util.getString(url, charset);
System.out.println("result=" + result);
}
/**
* 证书信任管理器类
*/
private class LmX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
/**
* 主机校验类
*/
private class LmHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}