Android - Connecting Https to a .NET WCF service gives an SSLException: "No peer certificate"

[SOLVED] - See below

I get an exception when trying to get some JSON data from the WCF service included in REST in Android 2.2 via an HTTPS connection. Then I noticed something very strange. When running the application on my phone, it worked perfectly and it would receive excellent JSON data. However, when I started the application on my emulator, it crashed as soon as "httpclient.execute (request)" was executed, providing an SLLException: no peer certificate. This is especially strange because my WCF service had a valid signed RapidSSL Certificate (CA). In addition, I made sure that client certificates are not needed inside my IIS. (Also, this function was not launched on UIThread !, it worked in a separate thread.)

My code is:

public Object getJSON(String URL, HashMap<String, String> parameters, String METHOD, String TAG) { try { this.TAG = TAG; // Send GET request to <service>/METHOD?params //Target API(8) for AndroidHttpClient and Target API(5) for DefaultHttpClient AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); String params = "?"; if(parameters != null){ for (Entry<String, String> para : parameters.entrySet()) { params = params + (String)para.getKey() + "=" + (String)para.getValue() + "&"; } params = params.substring(0, params.length() - 1); Log.i("U n P's", params);//Username and password check } HttpGet request = new HttpGet(URL + METHOD + params); Log.i(TAG, request.getRequestLine().toString()); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); HttpResponse response = client.execute(request);//Crashes here HttpEntity responseEntity = response.getEntity(); // Read response data into buffer char[] buffer = new char[(int)responseEntity.getContentLength()]; InputStream stream = responseEntity.getContent(); InputStreamReader reader = new InputStreamReader(stream); reader.read(buffer); stream.close(); 

Now I tried to find a solution, but basically this solution did not match the solution.

[DECISION]

My emulator was taking a snapshot since I was editing the hosts file, so I didn't have to install the service in DNS. Now here comes the trick, as I ran it through the snapshot every time. The date was set on OCT 24 2012 with a time of 18:48 in the emulator. Then I understood the date when I confirmed that the certificate was October 27, 2012 with a time of 14:45. Apparently, the option "get time from the Internet" does NOT work on the emulator. This caused my httpclient to throw the “No peer certificates” exception, since it did not have an approved certificate in the emulator. The sad part is that when you look for a solution to this exception with the keywords "android" or "https android wcf" you get only the results of people with problems associated with self-signed certificates.

TL DR: Apparently, verified certificates will not be accepted in the future.

+6
source share
1 answer

As stated by Arcshade.

My emulator was taking a snapshot since I was editing the hosts file, so I didn't have to install the service in DNS. Now here comes the trick, as I ran it through the snapshot every time. The date was set on OCT 24 2012 with a time of 18:48 in the emulator. Then I understood the date when I confirmed that the certificate was October 27, 2012 with a time of 14:45. Apparently, the option "get time from the Internet" does NOT work on the emulator. This caused my httpclient to throw the “No peer certificates” exception, since it did not have an approved certificate in the emulator. The sad part is that when you look for a solution to this exception with keywords like “android” or “https android wcf”, you get only the results of people with problems associated with self-signed certificates.

0
source

All Articles