使用 ksoap2-android 不受信任的证书
我正在使用 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;
}
I'm using ksoap2-android to make a call to wcf service over SSL. I can get it to work without SSL, but now I want to make the call over SSL, but I've run in to some problems.
I'm using the HttpsTransportSE instead of HttpTransportSE, but I'm getting the error:
javax.net.ssl.SSLException: Not trusted server certificate
How can I fix this?
Can I add the server certificate to the Keystore in Android to solve the problem?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了用一些源代码补充 Vedran 的答案,抱歉我无法发表评论。
trustManager:
调用您的方法:
注意:
构造如下:
[https://服务器:443/URL]
To complement the answer of Vedran with some source code, sorry I can't comment.
The trustManager:
The call at your method:
Notes:
Which is constructed as:
[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 服务迈出正确的第一步,这提高了生产力。
Well, there is an easier way to do this instead of modifying HttpsServiceConnectionSE. You can install a fake trust manager as described in http://groups.google.com/group/android-developers/browse_thread/thread/1ac2b851e07269ba/c7275f3b28ad8bbc?lnk=gst&q=certificate and then call allowAllSSL() before you do any SSL communication/call to ksoap2. It will register a new default HostnameVerifier and TrustManager. ksoap2, when doing its SSL communication, will use the default ones and it works like a charm.
You can also put some more effort into this, make it (much) safer, and install certificates in an application local trust manager, I guess. I was in a safe network and not afraid of man-in-the-middle-attacks so I just did the first.
I found it necessary to use KeepAliveHttpsTransportSE like this
new KeepAliveHttpsTransportSE(host, port, file, timeout);
. The parameters go into a URL object, so e.g. to access a Jira installation it's something likenew KeepAliveHttpsTransportSE("host.whatever", 443, "/rpc/soap/jirasoapservice-v2", 1000)
.Sometimes its handy if you are new to the technology or the web service you like to use to play around with it in a J2SE environment instead of in the emulator or even on the device, but in the J2SE/ME ksoap2 library the (KeepAlive)HttpsTransportSE stuff is missing (I used ksoap2-j2se-full-2.1.2.jar). What you could do is to get the sources for the three classes HttpsTransportSE, KeepAliveHttpsTransportSE, and HttpsServiceConnectionSE from the Android spin-off ksoap2-android and put them into your J2SE project and use them. It worked for me and it became a productivity improvement to get the first steps right with an unknown and quite complex web service.
对我有用
KSOAP + Web 服务 WCF 与 eclipse
Works for me
KSOAP + Web service WCF with eclipse
是的,也许你可以尝试一下
Https Connection Android
已提交一个错误有关此问题的问题跟踪器
http://code.google.com/p/ android/issues/detail?id=2388
Yes probably you can try this out
Https Connection Android
There has been a bug that has been filed on Issue Tracker regarding this
http://code.google.com/p/android/issues/detail?id=2388