1. 程式人生 > >android ksoap2 訪問https javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorExce

android ksoap2 訪問https javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorExce

android 使用ksoap2 訪問webservice時,若訪問的是https,報https javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException

解決方案:

SsX509TrustManager.allowAllSSL();

具體如下:

public static String getWebServiceResult(String nameSpace,

String methodName, String endPoint, String soapAction,

LinkedHashMap<String, String> params) {

// 指定WebService的名稱空間和呼叫的方法名

SoapObject rpc = new SoapObject(nameSpace, methodName);

// 設定需呼叫WebService介面需要傳入的兩個引數mobileCode、userId

for (String key : params.keySet()) {

System.out.println("Key = " + key + ", Value = " + params.get(key));

rpc.addProperty(key, params.get(key));

}

// 生成呼叫WebService方法的SOAP請求資訊,並指定SOAP的版本

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

envelope.bodyOut = rpc;

// 設定是否呼叫的是dotNet開發的WebService

envelope.dotNet = false;

// 等價於envelope.bodyOut = rpc;

envelope.setOutputSoapObject(rpc);

HttpTransportSE transport = new HttpTransportSE(endPoint);

SsX509TrustManager.allowAllSSL();//關鍵點-------------

String result = null;

try {

//transport.debug = true;

// 呼叫WebService

transport.call(soapAction, envelope);

// 獲取返回的資料

SoapObject object = (SoapObject) envelope.bodyIn;

// 獲取返回的結果

result = object.getProperty(0).toString();

} catch (Exception e) {

//e.printStackTrace();

if (e.toString().contains("ConnectException")) {

result = "ConnectException";

}

}

return result;

}