近日本人正在做一个小型的网络爬虫项目,用的就是经过分析,最终选择了jsoup来做页面分析工具,爬取数据。针对一般的http请求是不需要的校验的。但是https安全校验过总过不去。最后找到以下方法,终于成功。
让我们的站点信任所有站点,不需要引包,系统自带ssl证书校验,话不多数,贴代码。
/**
* 信任任何站点,实现https页面的正常访问
*
*/
public static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
// e.printStackTrace();
}
}
以下是引用的类,大家被搞错了。
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
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;
然后就是使用了 ,
在需要进行创建请求对象之前加入这个方法就行。
实例:
trustEveryone();
Connection conn = HttpConnection2.connect(url);
conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.header("Accept-Encoding", "gzip, deflate, br");
conn.header("Accept-Language", "zh-CN,zh;q=0.9");
conn.header("Cache-Control", "max-age=0");
conn.header("Connection", "keep-alive");
conn.header("Host", "blog.maxleap.cn");
conn.header("Upgrade-Insecure-Requests", "1");
conn.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36");
Document doc = null;
好了,然后就可以正常访问了。
亲测有效,这是目前我正在使用的方法。
欢迎关注公众号 IT技术自查 获取更多跟详细的IT情报