Android Use A Ksoap2 Request With Https
I have a problem to use a soap request with my app android. I use Ksoap2 library. this is my code: public class WebServiceCall { private static final String TAG = WebServiceCall.c
Solution 1:
I resolve this problem to modify my WebServiceCall:
public class WebServiceCall {
private static final String TAG = WebServiceCall.class.getSimpleName();
private static TrustManager[] trustManagers;
public static String callWSThreadSoapPrimitive(String strURL, String strSoapAction, SoapObject request) {
try {
StringBuffer result;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// certificat SSL
allowAllSSL();
HttpTransportSE ht = new HttpTransportSE(strURL);
List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
ht.debug = true;
ht.call(strSoapAction, envelope,llstHeadersProperty);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
result = new StringBuffer(response.toString());
Log.i(TAG, "result: " + result.toString());
return result.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
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());
}
Post a Comment for "Android Use A Ksoap2 Request With Https"