title: Android Emulator using HttpGet to acess RESTful web service through a proxy
body: <p>I'm trying to access a RESTful web service through the Android Emulator on my PC, which uses a proxy to connect to the internet.  </p>
  
  <p>I have code working fine to access the web service on an actual Android device that has its own data connection with the following code:</p>
  
  <pre><code>    DefaultHttpClient client = new DefaultHttpClient();
      client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  
      HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
      UsernamePasswordCredentials creds = 
          new UsernamePasswordCredentials("username", "password");
      request.addHeader(BasicScheme.authenticate(creds, "UTF-8", false));
  
      HttpResponse response = client.execute(request);
  </code></pre>
  
  <p>I've tried a number of approaches to try to get the Emulator to allow connection through the proxy, but none have worked.</p>
  
  <p>Note, I do have the INTERNET enabled in AndroidManifest.xml:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p>Attempt 1 - Setting Properties: </p>
  
  <p>This produces an UnknownHostException for the URL of my service at the execute() call</p>
  
  <pre><code>Properties props = System.getProperties();
  props.put("http.proxyHost", "httpproxy.mycompany.com");
  props.put("http.proxyPort", "80");
  </code></pre>
  
  <p>Attempt 2 - Setting the proxy in the DefaultHttpClient:</p>
  
  <p>This produces an UnknownHostException for the actual proxy</p>
  
  <pre><code>DefaultHttpClient client = new DefaultHttpClient();
  client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  
  HttpHost proxy = new HttpHost("httpproxy.mycompany.com", 80);
  client.getCredentialsProvider().setCredentials(
          new AuthScope("httpproxy.mycompany.com", 80),
          new UsernamePasswordCredentials("username", "password"));
  client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  
  HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
  UsernamePasswordCredentials cred = 
      new UsernamePasswordCredentials("username", "password");
  request.addHeader(BasicScheme.authenticate(cred, "UTF-8", false));
  HttpResponse response = client.execute(request);
  </code></pre>
  
  <p>Attempt 3 - Setting the proxy in the HttpGet</p>
  
  <p>This produces an UnknownHostException for the URL in my HttpGet</p>
  
  <pre><code>DefaultHttpClient client = new DefaultHttpClient();
  client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  
  HttpGet request = new HttpGet("http://mytesturl.com/services/serviceName");
  UsernamePasswordCredentials cred = 
      new UsernamePasswordCredentials("username", "password");
  request.addHeader(BasicScheme.authenticate(cred, "UTF-8", false));
  Header bs = new BasicScheme().authenticate(
          new UsernamePasswordCredentials("username", "password"),
          request);
  request.addHeader("Proxy-Authorization", bs.getValue());
  HttpResponse response = client.execute(request);
  </code></pre>
  
  <p>I'm not sure what else to try.  I'm open to any suggestions.  </p>