title: Java SSLException: hostname in certificate didn't match. Veryfing wrong certificate?
body: <p>I'm writing simple notepad for android that sync data over internet with my server. I used https and free certificate from startssl and everything worked smooth. Later I added new virtualhost with it's own ssl certificate and my app suddenly stoped connscting to server. It throws:</p>
  
  <pre><code>javax.net.ssl.SSLException: hostname in certificate didn't match: &lt;www.rapidnote.emendus.ovh&gt; != &lt;othersite.emendus.ovh&gt; OR &lt;othersite.emendus.ovh&gt; OR &lt;emendus.ovh&gt;
  </code></pre>
  
  <p>It tries to verify totally different certificate from different apache virtual host. My apache config for rapidnote virtual host is:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerName                rapidnote.emendus.ovh
    ServerAlias               www.rapidnote.emendus.ovh
    Redirect Permanent        /   https://www.rapidnote.emendus.ovh
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    ServerName                rapidnote.emendus.ovh
    DocumentRoot              /var/www/rapidnote
    SSLEngine                 on
    SSLCertificateFile        cert_location/rapidnote.emendus.ovh.crt
    SSLCertificateKeyFile     key_location/rapidnote.emendus.ovh.key
    SSLCertificateChainFile   cert_location/sub.class1.server.ca.pem
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
    ServerName                www.rapidnote.emendus.ovh
    DocumentRoot              /var/www/rapidnote
    SSLEngine                 on
    SSLCertificateFile        cert_location/www.rapidnote.emendus.ovh.crt
    SSLCertificateKeyFile     key_location/www.rapidnote.emendus.ovh.key
    SSLCertificateChainFile   cert_location/sub.class1.server.ca.pem
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>My code looks following:</p>
  
  <pre><code>HttpClient   httpClient = new DefaultHttpClient();
  HttpPost     httpPost   = new HttpPost("https://www.rapidnote.emendus.ovh/" + paramScriptName);
  HttpResponse response   = httpClient.execute(httpPost);
  </code></pre>
  
  <p>If i turn off <code>othersite.emendus.ovh</code> virtual host in apache, then SSLException message says:</p>
  
  <pre><code>&lt;www.rapidnote.emendus.ovh&gt; != &lt;rapidnote.emendus.ovh&gt; OR &lt;rapidnote.emendus.ovh&gt; OR &lt;emendus.ovh&gt; 
  </code></pre>
  
  <p>and if then I change <code>"https://www.rapidnote.emendus.ovh/"</code> to <code>"https://rapidnote.emendus.ovh/"</code>, it starts to work. I checked both (www. and non www) certificates and common names in both are correct.
  I think it should verify a certificate for domain that I gave him in <code>HttpPost</code> constructor, not some other random cert from my server...</p>
  
  <p>I used some testing website to verify my ssl configuration and it stated that it's all fine.</p>
  
  <p>Everything happend on <code>android 4.2.2 (API 17)</code>. 
  I build app with <code>min SDK 14</code> and <code>target SDK API 20 Android 4.4 (KitKat Wear)</code></p>
  
  <p>How can I tell him to verify correct cert, specified for this domain?
  Is the problem on site of application or apache?</p>