Android:为什么 HttpPost 请求不通过代理?
我在模拟器上设置了一个新的接入点,以便我可以按照此处的说明查看 Fiddler 中的流量: http://auir.wordpress.com/2010/03/22/tutorial-getting-android-emulator-working- with-fiddler-http-proxy-tool/
这适用于来自模拟器的浏览器请求,但我的应用程序中的 HttpPost 请求现在在 Fiddler 中可见。
这是我正在使用的代码:
private InputStream post(String url, Hashtable<String, String> postvariables) {
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri;
InputStream data = null;
try {
uri = 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;
}
知道我做错了什么吗?
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 {
uri = 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未明确尝试过此操作,但看到许多报告称通过更改 APN 重定向模拟器流量只会影响浏览器。使用选项
-http-proxy
运行模拟器实例可能会有更好的运气。请在模拟器启动选项(网络)下查看更多信息:http://developer. android.com/guide/developing/tools/emulator.html
$0.02:我们使用 Charles 进行调试Web 服务和以这种方式启动模拟器适用于所有流量。
希望有帮助!
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!