Awe*_*ome 6 ssl https android soap android-ksoap2
我正在使用ksoap2-android通过SSL调用wcf服务.我可以在没有SSL的情况下使用它,但现在我想通过SSL进行调用,但我遇到了一些问题.
我正在使用HttpsTransportSE而不是HttpTransportSE,但我收到错误:javax.net.ssl.SSLException:不受信任的服务器证书
我怎样才能解决这个问题?
我可以将服务器证书添加到Android中的Keystore来解决问题吗?
private static final String SOAP_ACTION = "http://example.com/Service/GetInformation";
private static final String METHOD_NAME = "GetInformation";
private static final String NAMESPACE = "http://example.com";
private static final String URL = "dev.example.com/Service.svc";
public static Result GetInformation()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo property = new PropertyInfo();
property.name = "request";
Request request =
new Request("12", "13", "Ben");
userInformationProperty.setValue(request);
userInformationProperty.setType(request.getClass());
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "Request",new Request().getClass());
HttpsTransportSE transport = new HttpsTransportSE(URL, 443, "", 1000);
//HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
transport.debug = true;
try
{
transport.call(SOAP_ACTION, envelope);
return Result.FromSoapResponse((SoapObject)envelope.getResponse());
}
catch (IOException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
EhT*_*hTd 11
用一些源代码补充Vedran的答案,对不起我无法发表评论.
trustManager:
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 boolean isClientTrusted(X509Certificate[] chain) {
return (true);
}
public boolean isServerTrusted(X509Certificate[] chain) {
return (true);
}
public X509Certificate[] getAcceptedIssuers() {
return (_AcceptedIssuers);
}
}
public static void allowAllSSL() {
javax.net.ssl.HttpsURLConnection
.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
javax.net.ssl.SSLContext context = null;
if (trustManagers == null) {
trustManagers = new javax.net.ssl.TrustManager[] { new _FakeX509TrustManager() };
}
try {
context = javax.net.ssl.SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
Log.e("allowAllSSL", e.toString());
} catch (KeyManagementException e) {
Log.e("allowAllSSL", e.toString());
}
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context
.getSocketFactory());
}
Run Code Online (Sandbox Code Playgroud)
你方法的电话:
allowAllSSL();
HttpsTransportSE httpsTransport = new HttpsTransportSE(Server,443, URL, 1000);
Run Code Online (Sandbox Code Playgroud)
笔记:
其构造如下:[https:// Server:443/URL]
好吧,有一种更简单的方法可以做到这一点,而不是修改 HttpsServiceConnectionSE。您可以按照http://groups.google.com/group/android-developers/browse_thread/thread/1ac2b851e07269ba/c7275f3b28ad8bbc?lnk=gst&q=certificate中所述安装假信任管理器,然后在执行任何 SSL 之前调用allowAllSSL()与 ksoap2 的通信/调用。它将注册一个新的默认 HostnameVerifier 和 TrustManager。ksoap2 在进行 SSL 通信时,将使用默认的 SSL 通信,并且它的工作方式就像一个魅力。
我想,您还可以为此投入更多精力,使其(更加)安全,并在应用程序本地信任管理器中安装证书。我处于一个安全的网络中,并不害怕中间人攻击,所以我只做了第一个。
我发现有必要像这样使用 KeepAliveHttpsTransportSE new KeepAliveHttpsTransportSE(host, port, file, timeout);。参数进入 URL 对象,因此例如要访问 Jira 安装,它类似于new KeepAliveHttpsTransportSE("host.whatever", 443, "/rpc/soap/jirasoapservice-v2", 1000).
有时,如果您对某项技术或 Web 服务不熟悉,您喜欢在 J2SE 环境中而不是在模拟器中甚至在设备上使用它,那么它会很方便,但在 J2SE/ME ksoap2 库中(KeepAlive)缺少 HttpsTransportSE 内容(我使用 ksoap2-j2se-full-2.1.2.jar)。您可以做的是从 Android 衍生品 ksoap2-android 获取 HttpsTransportSE、KeepAliveHttpsTransportSE 和 HttpsServiceConnectionSE 三个类的源代码,并将它们放入您的 J2SE 项目中并使用它们。它对我很有用,并且通过未知且相当复杂的 Web 服务迈出正确的第一步,这提高了生产力。
| 归档时间: |
|
| 查看次数: |
19229 次 |
| 最近记录: |