title: Secure communication between android application and ASP.net api
body: <p>My Android application is supposed to communicate with a ASP.net web api which is written in <code>C#.Net</code>. The data which is sent from the phone contains data that should not be exposed to the public. So I'm trying to use the <code>https</code> protocol. On my serverside I require all requests to be <code>HTTPS</code>, like this:</p>
  
  <p><strong>RequireRegisteredImei</strong></p>
  
  <pre><code>public class RequireRegisteredImeiAttribute : AuthorizationFilterAttribute
      {
          public override void OnAuthorization(HttpActionContext actionContext)
          {
              var request = actionContext.ControllerContext.Request;
  
              if (request.RequestUri.Scheme == Uri.UriSchemeHttps)
              {
                 //OKAY
              }
              else 
              {
               actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
              }
          }
       }
  </code></pre>
  
  <p><strong>And in the Controller</strong>: </p>
  
  <pre><code>[RequireRegisteredImei]
  public string Post()
  {
  }
  </code></pre>
  
  <p>I debugged this code by sending a simple <code>http request</code> from my phone, and this code works quite well, it will deny me. </p>
  
  <p>So, I started looking at how I could send requests over <code>https</code> from my android phone. I came up with something like this:</p>
  
  <pre><code>public static DefaultHttpClient getSecureHttpClient() {
          SchemeRegistry schemeRegistry = new SchemeRegistry();
          schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
          schemeRegistry.register(new Scheme("http", SSLSocketFactory.getSocketFactory(), 80));
          BasicHttpParams params = new BasicHttpParams();
          SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
          return new DefaultHttpClient(mgr, params);
      }
  </code></pre>
  
  <p>I'm using this method this way:</p>
  
  <pre><code>HttpClient httpClient = CustomHttpClient.getSecureHttpClient();
  </code></pre>
  
  <p>This will only result in an <code>IOException: No peer certificate</code></p>
  
  <p>I've read several threads regarding this:</p>
  
  <p><a href="https://stackoverflow.com/questions/9531710/problems-with-https-no-peer-certificate-in-android">Problems with https (No peer certificate) in android</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8806128/android-ssl-no-peer-certificate">Android SSL - No Peer Certificate</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9574870/no-peer-certificate-error-in-android-2-3-but-not-in-4">&#39;No peer certificate&#39; error in Android 2.3 but NOT in 4</a></p>
  
  <p>But there has to be a simpler way to post data over <code>HTTPS</code> from android? </p>