简体   繁体   中英

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.

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

您是否使用了-http-proxy http://:模拟器命令行选项或“设置”->“无线和网络”->“移动网络”->“访问点名称”->“ Telkila”或“主页”>“菜单”>设置>无线控件>移动网络>接入点名称?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM