次のURLのhtmlを解析しようとしています。
https://www.smuc.ac.kr/mbs/smuc/jsp/board/list.jsp?boardId=6993&id=smuc_0401000000
次のエラーが発生します:
Sun.security.validator.ValidatorException: PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at Sun.security.validator.PKIXValidator.doBuild(PKIXValidator.Java:387)
at Sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.Java:292)
at Sun.security.validator.Validator.validate(Validator.Java:260)
at Sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.Java:324)
at Sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.Java:229)
at Sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.Java:124)
at Sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.Java:1491)
... 15 more
Caused by: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at Sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.Java:141)
at Sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.Java:126)
at Java.security.cert.CertPathBuilder.build(CertPathBuilder.Java:280)
at Sun.security.validator.PKIXValidator.doBuild(PKIXValidator.Java:382)
... 21 more
これは私のコードです:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView textView;
public ArrayList<String> arrayList = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
new Insert().execute();
arrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_ok, R.id.text, arrayList );
}
class Insert extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
// Connection.Response res = Jsoup.connect("https://www.smuc.ac.kr/mbs/smuc/index.jsp")
// .method(Connection.Method.POST)
// .execute();
Document document = Jsoup.connect("https://www.smuc.ac.kr/mbs/smuc/jsp/board/list.jsp?boardId=6993&id=smuc_040100000000").get();
Elements elements = document.select(".tit");
arrayList.clear();
for (Element element : elements) {
arrayList.add(element.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
listView.setAdapter(arrayAdapter);
}
}
}
TSL検証は無視して、validateTLSCertificates(false)
を設定できます。
_Document document = Jsoup.connect("URL").timeout(10000).validateTLSCertificates(false).get();
_
ページの読み取りにも時間がかかるため、タイムアウトtimeout(10000)
を増やしてください。
選択した回答は、validateTLSCertificatesが非推奨になり削除されたため、JSoupの最新リリースでは機能しません。次のヘルパークラスを作成しました。
public class SSLHelper {
static public Connection getConnection(String url){
return Jsoup.connect(url).sslSocketFactory(SSLHelper.socketFactory());
}
static private SSLSocketFactory socketFactory() {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public Java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new Java.security.SecureRandom());
SSLSocketFactory result = sslContext.getSocketFactory();
return result;
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Failed to create a SSL socket factory", e);
}
}
}
それから私はそれを単に次のように呼びます:
ドキュメントdoc = SSLHelper.getConnection(url).userAgent(USER_AGENT).get();
(*)- https://dzone.com/articles/how-setup-custom -解決策を考え出すのに役立ちます