仅在Google Cloud的tomcat上运行时,我才有问题无法通过改造来获取我的网站。当我在计算机上运行它时,一切正常。
它始于Sun.security.validator.ValidatorException: PKIX path validation failed
的Google云。因此,我发现您可以在改造时禁用ssl验证,因为这只是为了抓取内容。
所以我添加了以下代码:
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier((hostname, session) -> true);
builder.addInterceptor(interceptor);
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
一切仍然可以在我自己的计算机上运行,但是在外部tomcat上,它仍然使我找不到http 404。