Android HttpClient 不使用系统代理设置

发布于 2024-10-08 19:58:18 字数 415 浏览 5 评论 0原文

当我创建 DefaultHttpClient 对象并尝试点击网页,请求不会通过我在“设置”中指定的代理进行路由。

浏览 API 文档,我没有看到任何可以指定代理的地方,尽管 Android 确实有 Proxy 类,允许我读取系统的代理设置。

有没有办法可以在 HttpClient 中使用代理设置?

When I create a DefaultHttpClient object and try to hit a webpage, the request isn't routed through the proxy I specified in Settings.

Looking through the API docs, I don't see anywhere where I can specify a proxy though Android does have a Proxy class that allows me to read the system's proxy settings.

Is there a way I can use the proxy settings in an HttpClient?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

鹿港小镇 2024-10-15 19:58:18

尝试:(

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpHost proxy = new HttpHost("someproxy", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

此处挑选)

Try:

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpHost proxy = new HttpHost("someproxy", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

(culled from here)

画骨成沙 2024-10-15 19:58:18

首先,我将确保请求符合您在 Android 设备设置中设置的代理设置属性。您可以通过查看 android.provider.Settings 中的 System 类来通过代码确定这一点;

要确定用户是否有系统代理设置,您可以执行以下操作:

    System.getProperty("http.proxyHost");
    System.getProperty("http.proxyPort");

    System.getProperty("https.proxyHost");
    System.getProperty("https.proxyPort");

如果您有 DefaultHTTPClient 的实例,那么您可以检查它是否也有相关的代理设置。

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);

这些都是“获取”代理设置的方法,并且“设置”方法以相同的方式实现,通过 System.setProperty 或 httpclient.setParams。

希望这有帮助!

Firstly, I would make sure that the request is adhering to the proxy settings properties you set in the Android Device's settings. You can determine this via code by looking at the System class in android.provider.Settings;

To identify if the user had system proxy settings, you can do the following:

    System.getProperty("http.proxyHost");
    System.getProperty("http.proxyPort");

    System.getProperty("https.proxyHost");
    System.getProperty("https.proxyPort");

If you have an instance of DefaultHTTPClient, then you can check whether it has the relevant proxy settings as well.

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);

These are all ways to 'get' the proxy settings, and the 'set' methods are implemented in the same way, either through System.setProperty or httpclient.setParams.

Hope this helped!

情徒 2024-10-15 19:58:18

我正在开发 Android 代理库,尝试抽象每个 Android 版本对代理设置的访问权限。您可以轻松获取用户当前选择的代理设置。

I'm developing the Android Proxy Library that try to abstract the access to proxy settings for every Android version. You can easily get the proxy settings currently selected by the user.

鹿港巷口少年归 2024-10-15 19:58:18

尝试:

System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost httpproxy = new HttpHost("<your proxy host>",<your proxy port>);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  httpproxy);

HttpHost proxy = new HttpHost("ip address",port number);  
DefaultHttpClient httpclient = new DefaultHttpClient(); 
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param name", param));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity(); 
System.out.println("Request Handled?: " + response.getStatusLine());
InputStream in = entity.getContent();
httpclient.getConnectionManager().shutdown();

Try :

System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);

or

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost httpproxy = new HttpHost("<your proxy host>",<your proxy port>);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  httpproxy);

or

HttpHost proxy = new HttpHost("ip address",port number);  
DefaultHttpClient httpclient = new DefaultHttpClient(); 
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param name", param));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity(); 
System.out.println("Request Handled?: " + response.getStatusLine());
InputStream in = entity.getContent();
httpclient.getConnectionManager().shutdown();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文