반응형
public static String httpsGet(String strURL) throws Exception
{
URL url = null;
HttpsURLConnection con = null;
String ret = new String();
try {
url = new URL(strURL);
ignoreSsl();
con = (HttpsURLConnection)url.openConnection();
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input = null;
while ((input = br.readLine()) != null){
ret += input;
}
br.close();
}
catch (IOException e) {
ExceptionUtil.getStackTrace(e);
} finally {
if (con != null) {
con.disconnect();
}
}
return ret;
}
public static void ignoreSsl() throws Exception{
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session)
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager,X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
}
반응형
'[Java] > [Java Code]' 카테고리의 다른 글
[Java Code] 파일 다운로드 구현하기 (0) | 2022.09.04 |
---|---|
[Java Code] 현 OS의 CPU, MEMORY, HDD의 사용량 및 남은용량 구하기 (0) | 2022.09.04 |
[Java Code] HashMap 을 JSONObject로 변환 / ArrayList를JSONArray 로 변환 (0) | 2022.08.02 |
[Java Code] 특정 단어 모든 위치 찾기 (0) | 2022.08.01 |
[Java Code] 두 위경도 좌표로 직선 거리 구하기 (0) | 2022.06.30 |