아래 코드를 작성했습니다. 인증서를 성공적으로 가져 왔습니다. 아무도 예외를 얻는 이유가 무엇인지 말해 줄 수 있습니까? 하나의 IP https://x.x.x.x을 연결하려고하는데 예외가 발생했습니다. 나는 두 가지를 시도했다javax.net.ssl.SSLHandshakeException : java.security.cert.CertificateException
1) 인증서를 전달함으로써 2) 인증서를 다운로드하고 keytool을 사용하여 java 트러스트 스토어에 추가한다. 나를 위해 아무것도 잘 작동하지 않습니다.
package com.dell;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
URL obj = null;
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
}
try {
obj = new URL("https://10.94.218.114");
} catch (MalformedURLException e) {
}
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
예외를 모두 표시 할 수 있습니까? 인증서에 무엇이 잘못되었는지 표시해야합니다. – Thilo
스레드 "main"의 예외 javax.net.ssl.SSLHandshakeException : java.security.cert.CertificateException : 주제 대체 이름이 없습니다. \t at sun.security.ssl.Alerts.getSSLException (알 수없는 소스) \t at sun.security .ssl.SSLSocketImpl.fatal (알 소스)에 sun.security.ssl.Handshaker.fatalSE \t (알 소스)에 sun.security.ssl.Handshaker.fatalSE \t (알 소스)에 \t sun.security.ssl .ClientHandshaker.serverCertificate (알 수없는 출처) \t (sun.security.ssl.ClientHandshaker.processMessage) 알 수없는 원본) – amiton2006
발생 원인 : java.security.cert.CertificateException : 주제 대체 이름이 없습니다. \t at sun.security.util.HostnameChecker.matchIP (알 수없는 소스) – amiton2006