Android: Why is HttpPost request not going through proxy?
I've set up a new Access Point on my emulator so that I can view traffic in Fiddler by following the instructions here: http://aurir.wordpress.com/2010/03/22/tutorial-getting-android-emulator-working-with-fiddler-http-proxy-tool/
This works for the browser requests from the Emulator but the HttpPost request in my application is now visible in Fiddler.
Here's the code I'm using:
private InputStream post(String url, Hashtable<String, String> postvariables) {
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri;
InputStream data = null;
try {
u开发者_高级运维ri = new URI(url);
HttpPost method = new HttpPost(uri);
method.setHeader("Content-Type","application/json");
String param = new String();
Enumeration<String> e = postvariables.keys();
while(e.hasMoreElements())
{
String key = e.nextElement();
param = param + key + "=" + postvariables.get(key);
if(e.hasMoreElements()) {
param = param + "&";
}
}
Log.i("RestClient",url + param);
HttpEntity entity = new StringEntity(param);
method.setEntity(entity);
HttpResponse response = httpClient.execute(method);
data = response.getEntity().getContent();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
Any idea what I'm doing wrong?
I have never tried this explicitly, but have seen many reports that redirecting emulator traffic by changing the APN only affects the Browser. You might have better luck running the emulator instance with the option -http-proxy <proxy>
. Look here, under Emulator Startup Options (Network) for more:
http://developer.android.com/guide/developing/tools/emulator.html
$0.02: We use Charles to debug web services and booting up the emulator in this fashion works for all traffic.
Hope that helps!
精彩评论