Josh Falter
Josh Falter

Reputation: 21

Android Emulator using HttpGet to acess RESTful web service through a proxy

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.

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:

    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);

I've tried a number of approaches to try to get the Emulator to allow connection through the proxy, but none have worked.

Note, I do have the INTERNET enabled in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Attempt 1 - Setting Properties:

This produces an UnknownHostException for the URL of my service at the execute() call

Properties props = System.getProperties();
props.put("http.proxyHost", "httpproxy.mycompany.com");
props.put("http.proxyPort", "80");

Attempt 2 - Setting the proxy in the DefaultHttpClient:

This produces an UnknownHostException for the actual proxy

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);

Attempt 3 - Setting the proxy in the HttpGet

This produces an UnknownHostException for the URL in my HttpGet

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);

I'm not sure what else to try. I'm open to any suggestions.

Upvotes: 1

Views: 1524

Answers (2)

RichardSmithONS
RichardSmithONS

Reputation: 31

Having the same problem, I succeeded with a variation on attempt 3 (code below), the cruicial difference being the setProperty statements. Note that the web service I am calling does not require authentication so I'm only setting the proxy authorization header.

System.setProperty("java.net.useSystemProxies", "false");
System.setProperty("http.proxyHost", "123.56.7.9");
System.setProperty("http.proxyPort", "8080"); 

DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

HttpGet request = new HttpGet("web service url");
Header bs = new BasicScheme().authenticate(
    new UsernamePasswordCredentials("NETWORKID", "netpassword"),
    request);
request.addHeader("Proxy-Authorization", bs.getValue());
HttpResponse response = client.execute(request);

Upvotes: 1

Luca
Luca

Reputation: 4273

did you use -http-proxy http://: emulator command line option or "Settings" -> "Wireless & Networks" -> "Mobile Networks" -> "Access Point Names" -> "Telkila" or Home > Menu > Settings > Wireless Controls > Mobile Networks > Access Point Names?

Upvotes: 0

Related Questions