如何通过代理进行所有网络流量? [英] How to make all network traffic go via a proxy?

查看:1010
本文介绍了如何通过代理进行所有网络流量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序向远程服务器发出http请求。我使用以下代码执行此操作:

I have an app that makes http requests to a remote server. I do this with the following code:

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("myURL");

    try {

        ArrayList<BasicNameValuePair> postVariables = new ArrayList<BasicNameValuePair>(2);
        postVariables.add(new BasicNameValuePair("key","value"));

        httpPost.setEntity(new UrlEncodedFormEntity(postVariables));
        HttpResponse response = httpClient.execute(httpPost);
        String responseString = EntityUtils.toString(response.getEntity());

        if (responseString.contains("\"success\":true")){
            //this means the request succeeded
        } else {
            //failed
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

这很顺利,但我们的一位客户设置了一个APN,需要通过某个代理服务器的请求。如果我将以下内容添加到请求中,则该请求将通过代理重新路由到服务器:

This goes really well, but one of our customers has set up an APN that requires requests to go via a certain proxy server. If I add the following to the request this works, the request gets rerouted via the proxy to the server:

    HttpHost httpHost = new HttpHost("proxyURL",8080);
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);

到目前为止,我还使用了一个可以发出一些http请求的库。库的代码对我来说是不可访问的,所以我不能将这两行添加到代码中。我联系了该库的创建者,他们告诉我应该可以设置android环境,以便所有请求都会自动通过代理。有类似的东西吗?我没有在谷歌上找到任何东西。

我基本上正在寻找一种方法来将上述两行设置为所有http请求的标准。请注意,APN不会将代理设置为整个手机的默认设置,因此应用程序必须手动执行此操作(是的,这意味着大多数应用程序无法在该客户的手机上运行)。

So far so good, however, I use a library that makes some http requests as well. The library's code is not accesible for me, so I can't add those two lines to the code. I contacted the creators of that library, and they told me it should be possible to set up the android environment so that all requests will automatically go through the proxy. Is there something like that? I didn't find anything on google.
I'm basically looking for a way to set the above two lines as a standard for all http requests. Please note that the APN does not set the proxy as a default for the entire phone, so apps will have to do this manually (and yes that means the majority of the apps don't work on that customer's phone).

推荐答案



因为我需要使用它已经一两年了,但如果我没记错的话,您可以使用 System.setProperty(String,String),以便为应用程序设置环境范围的设置,以便通过代理路由所有HTTP流量。您需要设置的属性是http.proxyHost和http.proxyPort,然后正常使用您的HttpClient而不指定代理,因为VM将处理路由请求。

文档了解更多信息关于我在说什么可以在这里找到: ProxySelector(只是让你知道什么要使用的密钥)这里有关实际 System.setProperty(字符串,字符串)函数的文档,如果没有不适合你,让我知道,我会尝试挖掘出设置系统级代理的旧代码。顺便说一下,它实际上只是系统级,因为每个应用程序都在自己的Dalvik中运行,所以你不会影响其他应用程序的网络通信。


It's been a year or two since I've needed to use it, but if I remember correctly, you can use the System.setProperty(String, String) in order to set an environment-wide setting for your application to route all HTTP traffic through a proxy. The properties that you should need to set are "http.proxyHost" and "http.proxyPort" and then use your HttpClient normally without specifying a proxy because the VM will handle routing requests.
Docs for more information about what I'm talking about can be found here: ProxySelector (just so you know what keys to use) and here for documentation about the actual System.setProperty(String, String) function
If that doesn't work for you, let me know and I'll try to dig out my old code that set a system-level proxy. BTW, it's really only "system-level" since each app runs in it's own Dalvik so you won't impact other app's network communications.

这篇关于如何通过代理进行所有网络流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆