Hi all, I'm new to Android development, so please forgive me if this is a dumb question.
I am currently connecting to a URL using a HttpsURLConnection (which is working fine) and I need to implement a custom IHostnameVerifier object, in order to make sure the Host name details of the certificate I am receiving, matches the one I expect. I have tried to achieve this by creating a class that implements the Interface Javax.Net.Ssl.IHostnameVerifier i.e.:
public class CustomHostnameVerifier : Javax.Net.Ssl.IHostnameVerifier
{
public bool Verify(string hostname, Javax.Net.Ssl.ISSLSession session)
{
if (hostname == "Expected Host")
return true;
else
return false;
}
public void Dispose()
{ }
public IntPtr Handle { get; set; }
}
However, when I try to assign this as the HostnameVerifier for my HttpsURLConnection using the following code:
Java.Net.URL urlObj = new Java.Net.URL(uri);
Javax.Net.Ssl.HttpsURLConnection urlConn = (Javax.Net.Ssl.HttpsURLConnection)urlObj.OpenConnection();
urlConn.HostnameVerifier = new CustomHostnameVerifier();
I receive the following Exception:
{Java.Lang.IllegalArgumentException: Exception of type 'Java.Lang.IllegalArgumentException' was thrown.
at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00063] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:507
at Javax.Net.Ssl.HttpsURLConnection.set_HostnameVerifier (IHostnameVerifier value) [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Javax.Net.Ssl.HttpsURLConnection.cs:165
at Uda.SmartApp.Data.Services.HttpRequest`2+<>c__DisplayClass2[Uda.SmartApp.Data.DataContracts.Application.Version,Uda.SmartApp.Data.DataContracts.Registration.Settings].<BeginPostRequestInternal>b__0 (System.Object state) [0x0001a] in d:\sourcecode\SACommon\Smart App - Common 1.0 PD2807 - Address Smart App Issues\Uda.SmartApp\Data\Services\HttpRequest.cs:120
--- End of managed exception stack trace ---
java.lang.IllegalArgumentException: HostnameVerifier is null
at javax.net.ssl.HttpsURLConnection.setHostnameVerifier(HttpsURLConnection.java:265)
at dalvik.system.NativeStart.run(Native Method)
}
I have tried searching Google for the correct way to implement this on Mono for Android, but I haven't had any luck.
Can someone please give me some guidance as to what I am doing wrong?
Thanks