title: 400 bad request invalid hostname only in android application
body: <p>I have a asp.net mvc website deployed on a server, providing a few web interfaces to others. For example, getting the current user's information, my test C# console application looks like this:</p>
  
  <pre><code>using (var client = new WebClient())
  {
       try
       {
           var url = "http://api.fake.mysite.com/v1.0/user/current";
           var token = "e0034e1c082de62b74e361b15f9c6471";
           var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(token));
           client.Headers["Authorization"] = encoded;
           client.Headers["Content-Type"] = "application/json";
           Console.WriteLine(client.DownloadString(url));
       }
       catch (WebException e)
       {
           //log the exception
       }
  }
  </code></pre>
  
  <p>You can see the usage is pretty simple, just request the url via HTTP_GET, set the <em>Authorization</em> header to the encoded token. Actually it works fine in my machine. But some one else meets a strange issue when visiting this url in an android application, here is the java code:</p>
  
  <pre><code>HttpClient httpClient = new DefaultHttpClient();
  String token = "e0034e1c082de62b74e361b15f9c6471";
  String url = "http://api.fake.mysite.com/v1.0/user/current";
  HttpGet httpGet = new HttpGet(url);
  String encoded = Base64.encodeToString(token.getBytes(), Base64.DEFAULT);
  httpGet.addHeader("Authorization", encoded);
  httpGet.addHeader("Content-Type", "application/json");
  
  try {    
      HttpResponse httpResponse = httpClient.execute(httpGet);
      int responseCode = httpResponse.getStatusLine().getStatusCode();
      String response = EntityUtils.toString(httpResponse.getEntity());
  } catch (ClientProtocolException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>then he got "400 bad request invalid host name" error. I've tried:</p>
  
  <p>(1) make sure the variable "encoded" has the same value in C# and Java code.</p>
  
  <p>(2) make sure the website's domain name is correctly set in server IIS</p>
  
  <p>(3) all PCs/mobile phones can visit the test index page(<a href="http://api.fake.mysite.com" rel="nofollow">http://api.fake.mysite.com</a>)</p>
  
  <p>(4) ping api.fake.mysite.com works fine</p>
  
  <p>(5) if removing <code>httpGet.addHeader("Authorization", encoded);</code>, the Java program got a 401 Unauthorized result as expected(the server code under my control returns the result)</p>
  
  <p>(6) some other applications using C# and PHP can use the web methods well, only android application can't(tested in two totally different android mobile phones, the android emulator got 400 invalid host name either)</p>
  
  <p>(7) use IP instead of domain name <a href="http://xx.xx.xx.xx/v1.0/user/current" rel="nofollow">http://xx.xx.xx.xx/v1.0/user/current</a>, everything is the same. (xx.xx.xx.xx stands for the ip address)</p>
  
  <p>(8) checked the IIS log, all requests to /v1.0/user/current returns 200/401/500, no 400 results.</p>
  
  <p>(9) make sure the android application has internet permissions(actually we've added all permissions)</p>
  
  <p>Does anyone know the reason or help to find the reason? Thank you very much, this issue is driving me crazy.</p>