Neo*_*gma 16
再次检查这个问题,我发现了一个更清洁的解决方案.无需修改KSOAP2文件.
在您的项目中,链接ksoap2-android-assembly-3.0.0-jar,没有任何修改.
接下来,创建一个SSLConnection.java使用以下代码命名的文件:
package com.example.mypackage;
import android.util.Log;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class SSLConection {
private static TrustManager[] trustManagers;
public static class _FakeX509TrustManager implements javax.net.ssl.X509TrustManager {
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return (_AcceptedIssuers);
}
}
public static void allowAllSSL() {
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
javax.net.ssl.SSLContext context;
if (trustManagers == null) {
trustManagers = new TrustManager[]{new _FakeX509TrustManager()};
}
try {
context = javax.net.ssl.SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
Log.e("allowAllSSL", e.toString());
} catch (KeyManagementException e) {
Log.e("allowAllSSL", e.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后SSLConection.allowAllSSL();在通过KSOAP2调用服务器方法之前调用.这一切,对我有用.所有SSL证书都被接受,我可以使用KSOAP2和https协议.
zir*_*ael 10
我还不能发表评论,所以我在这里发表评论rallat答案.他的解决方案有效但需要进一步解释.用ssl运行ksoap2:
ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar一个项目HttpTransportSE.java,ServiceConnectionSE.java(我还需要复制Transport.java,ServiceConnection.java和HeaderProperty.java).从这些文件中删除导入并确保它们使用您的文件(而不是从中导入ksoap2.jar)使用rallat答案(我复制粘贴):
ServiceConnectionSE.java 添加此项以接受不受信任的证书:
private 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) {
}
}
};
Run Code Online (Sandbox Code Playgroud)然后使用此构造函数来允许不受信任的证书和未验证的主机名:
public ServiceConnectionSE(String url) throws IOException {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.getMessage();
}
connection = (HttpsURLConnection) new URL(url).openConnection();
((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
}
Run Code Online (Sandbox Code Playgroud)第二个构造函数
public ServiceConnectionSE(Proxy proxy, String url) throws IOException {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.getMessage();
}
connection = (HttpsURLConnection) new URL(url).openConnection();
((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
}
Run Code Online (Sandbox Code Playgroud)在你的代码中使用:
HttpTransportSE aht = new HttpTransportSE(URL);
aht.call(SOAP_ACTION, envelope);
Run Code Online (Sandbox Code Playgroud)教程中的其他内容
| 归档时间: |
|
| 查看次数: |
25462 次 |
| 最近记录: |