I'm developing an application that uses a SOAP API, and I'm using the ksoap2 library within Kotlin. I need to make an HTTPS request. I have a .pfx certificate with a specific password, and I also have a username/password that serve as credentials. In WcfStorm, the request works fine after downloading the certificate and configuring the username/password authentication at the security level with the TLS protocol in the configuration values. Now I've tried to implement this in code, but I’m stuck and don't know how to proceed. Has anyone encountered a similar problem, and how did you resolve it? Please provide a detailed explanation, as I’m a junior developer. Below is my current code.
Example function from the SoapClient.kt:
fun getSomething(context: Context): List<String> { val namespace = "NAMESPACE" val methodName = "GetSomething" val soapAction = "$namespace/FIXEDVALUE/$methodName" val urlString = getServiceUrl(context) val request = SoapObject(namespace, methodName) val username = "USERNAME" val password = "PASSWORD" request.addProperty("username", USERNAME) request.addProperty("password", PASSWORD) val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11).apply { dotNet = true setOutputSoapObject(request) } val headerList = listOf( HeaderProperty("soapAction", soapAction) ) try { val url = URL(urlString) val keyStore = KeyStore.getInstance("PKCS12").apply { load(context.resources.openRawResource(R.raw.CERTIFIKAT), "SIFRA".toCharArray()) } val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).apply { init(keyStore, "SIFRA".toCharArray()) } val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply { init(keyStore) } val sslContext = SSLContext.getInstance("TLS").apply { init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, null) } val transport = CustomHttpsTransportSE(url.host, url.port, url.file, 60000, sslContext.socketFactory) transport.debug = true transport.call(soapAction, envelope, headerList) val response = envelope.response as SoapObject return response } catch (e: Exception) { e.printStackTrace() return emptyList() }}
CustomHttpsTransportSE.kt:
class CustomHttpsTransportSE( host: String, port: Int, file: String, timeout: Int, private val sslSocketFactory: SSLSocketFactory) : HttpsTransportSE(host, port, file, timeout) {}
I receive 500 http status in line: transport.call(soapAction,envelope,headerList)
I've tried numerous things, but I haven't found an exact solution to this problem anywhere, nor complete code that could help me.