I am trying to send data using httpsurlconnection. I imported .pem certificate into android keystore and created ssl context. Now everything is working fine except when I send some data over server from Android it is saying hostname is not verified.
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
AssetManager assetManager = MainActivity.ctx.getAssets();
InputStream requestXML = assetManager.open("requestdump");
String requestString = convertStreamToString(requestXML);
java.net.URL url = new URL("https://nn.nnn.nnn.nnn:xxxx");
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(getSSLSocketFactory());
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setHostnameVerifier(hostnameVerifier );
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
byte[] outputInBytes = requestString.getBytes("UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write( outputInBytes );
os.close();
InputStream in = urlConnection.getInputStream();
This code is working and sending data on ssl enable server. I tried this code:
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
urlConnection.setHostnameVerifier(hostnameVerifier );
But I don't want to use this approach.
Without this approach I am getting Hostname not verified. Can anybody suggest please how to send data over server using self signed certificate without using ALLOW_ALL_HOSTNAME_VERIFIE.
Any help would be appreciated.