Android:为什么 HttpPost 请求不通过代理?

发布于 2024-10-18 06:49:15 字数 1440 浏览 2 评论 0原文

我在模拟器上设置了一个新的接入点,以便我可以按照此处的说明查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2024-10-25 06:49:15

我从未明确尝试过此操作,但看到许多报告称通过更改 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文