Java) jsoup.connect https / jsoup https 연결

북마크 추가

1. Jsoup 사용시 https에 연결하는 방법입니다.

Jsoup.connect(url).get(); 을 호출 하기 전에 아래 코드를 넣어줍니다. 

 

        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return new X509Certificate[0];}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};

SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

 

 

2.전체 코드

 

/**
* Created by Trandent on 2017. 1. 25..
*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class Main {

public static void main(String[] args) throws Exception {
try {
new Main().crawler("http://trandent.com");
} catch (Exception e) {
e.printStackTrace();
}

}


private void crawler(String domain) throws Exception {

TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return new X509Certificate[0];}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};

SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

Document doc = null;
try{
doc = Jsoup.connect(domain).get();
}catch(Exception e){
e.printStackTrace();
}
}
}

 

HKH
2017-01-25 14:34
SHARE
keyboard_arrow_left
keyboard_arrow_up