我正在尝试使用JAVA和KSOAP2为Android编写Web服务。 SOAP是我可以使用的唯一协议,ReST不是一个选项。
因此,我成功创建了SOAP请求并使用HTTP连接到服务器。但是,我需要HTTPS,因为敏感信息将被转移。禁用证书检查不是一个选项,因为数据是敏感的,我必须使用SSL。
由于Android在HTTPS中引发了认证错误,因此我在
之后创建了自己的密钥库1- http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
并将其添加到项目中。
我的代码类似于
2- http://www.techques.com/question/1-4646121/Not-trusted-certificate-using-ksoap2-android。
我也经历过
4 - Apache HttpClient on Android producing CertPathValidatorException (IssuerName != SubjectName)
但不能直接使用它们。
将非常感谢使用HTTPS显示与2中的代码相关的1中的代码的伪代码。
2中的最后评论实际上意味着什么?他在代码中使用了HttpsTransportSE,但他说他扩展了HttpsServiceConnectionSE。你能在伪代码中显示这个吗?
另外,我应该使用HttpsTransportSE或HttpsServiceConnectionSE来提供我将要连接的URL。
答案 0 :(得分:0)
使用eclipse为我提供SOAP + Web服务WCF。 带有证书和登录名/密码的Tomcat
可能这可以帮到你
private static SoapObject getBody(final SoapSerializationEnvelope soapEnvelope) throws Exception {
if (soapEnvelope.bodyIn == null) {
throw new Exception("soapEnvelope.bodyIn=null");
}
else if (soapEnvelope.bodyIn.getClass() == SoapFault.class) {
throw new ExceptionLogic((SoapFault) soapEnvelope.bodyIn));
}
else {
return (SoapObject) soapEnvelope.bodyIn;
}
}
private static SoapSerializationEnvelope sendRequete(final SoapObject soapReq, final String classMappingName,
final Class<?> classMapping, final int timeOutSpecial) {
final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.implicitTypes = true;
soapEnvelope.dotNet = true;
if (classMappingName != null) {
soapEnvelope.addMapping(NAMESPACE, classMappingName, classMapping);
}
soapEnvelope.setOutputSoapObject(soapReq);
try {
final HttpTransportSE httpTransport = new HttpTransportSE(Constante.urlWebService, timeOutSpecial);
httpTransport.debug = BuildConfig.DEBUG;
// Prod
if (Constante.urlWebService.startsWith("https://")) {
final List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
headerList.add(new HeaderProperty("Authorization", "Basic "
+ org.kobjects.base64.Base64.encode((Constante.CERTIFICAT_LOGIN + ":" + Constante.CERTIFICAT_MDP).getBytes())));
FakeX509TrustManager.allowAllSSL();
httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope, headerList);
}
// Test
else {
httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope);
}
return soapEnvelope;
}
catch (final Exception e) {
throw new Exception("Erreur : " + e.getMessage(), e);
}
}
private static class FakeX509TrustManager implements X509TrustManager {
private static TrustManager[] trustManagers;
private final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
@Override
public X509Certificate[] getAcceptedIssuers() {
return _AcceptedIssuers;
}
public static void allowAllSSL() {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(final String hostname, final SSLSession session) {
return true;
}
});
SSLContext context = null;
if (trustManagers == null) {
trustManagers = new TrustManager[] { new FakeX509TrustManager() };
}
try {
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
}
catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (final KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
@Override
public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {
}
@Override
public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
}
}