7
當我想打開一個HTTPS連接時,我得到SSL異常。如何設置HttpURLConnection以便對此異常不敏感?禁用HTTPS連接的SSL證書驗證?
我的代碼是:
private String getData() {
String response = null;
String connection = "https://www.kamalan.com/";
try {
URL url = new URL(connection);
Log.i(TAG, "Try to open: " + connection);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
Log.i(TAG, "Response code is: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);
// get returned message and show it
response = strBuilder.toString();
Log.i("JSON returned by server:", response);
}
in.close();
} else {
Log.e(TAG, "Couldn't open connection in getResepiItems()");
}
} catch (SSLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
什麼是logcat的說,下面的方法是什麼? –
@Morrison Chang這是log cat中的錯誤'導致:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:未找到認證路徑的信任錨'。 –
你看到這個SO帖子:http: //stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection? –