In Android N for SSL certificate i have to add this code-(according to given android developer link)
<?xml version="1.0" encoding="utf-8"?> <manifest ... > <application android:networkSecurityConfig="@xml/network_security_config" ... > ... </application> And a file network_security_config.xml in xml folder-
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config> <domain includeSubdomains="true">example.com</domain> <trust-anchors> <certificates src="@raw/my_ca"/> </trust-anchors> </domain-config> This is working fine for one static domain but my problem is-The server domain will not same every-time in my application. Second problem is i am downloading SSL certificate from my server domain at run time so how can i update certificate file in raw folder every time because we know we can't write file in raw folder at run-time.
so for dynamic flow how can i connect to my server in Android N with different different certificates.
Edit1: If i am not using this config file code, my app communication stopped from server so i need to use this code anyway.
Edit2: This is my code in which i am connecting to my server and its giving me response 200 mean ok without changing code for android N (Old code).
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); InputStream caInput = new BufferedInputStream(new FileInputStream(certFile)); X509Certificate ca =(X509Certificate) cf.generateCertificate(caInput); String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); // Create a TrustManager that trusts the CAs in our KeyStore String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; // Create an SSLContext that uses our TrustManager HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier()); SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, trustAllCerts, new java.security.SecureRandom()); // Tell the URLConnection to use a SocketFactory from our SSLContext url = new URL(wsdlUrl); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setSSLSocketFactory(sslcontext.getSocketFactory()); urlConnection.setConnectTimeout(5000); urlConnection.connect(); if (urlConnection.getResponseCode() == 200) { //Successful response. result = true; } else { result = false; } but on 200 response i am calling my SOAP service method to that server, in this case i am getting exception
com.neurospeech.wsclient.SoapFaultException: Server Error at com.neurospeech.wsclient.SoapWebService.postXML(SoapWebService.java:225) at com.neurospeech.wsclient.SoapWebService.getSoapResponse(SoapWebService.java:157) at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync.access$1300(AndroidServiceAsync.java:6) at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync$setAndroidClient HeartbitRequest.executeRequest(AndroidServiceAsync.java:367) at com.neurospeech.wsclient.ServiceRequest.run(ServiceRequest.java:20) at java.lang.Thread.run(Thread.java:761) Means my code of connnection url is working fine in Android N also but SOAP service giving me exception.
I think i didn't well defined my problem but please anyone who try please give me any clue.