how to use proxy for HTTP connection on android?

2019-04-01 16:46发布

问题:

The usual java way doesnt seem to work - i put in bogus values in there and it still "works", so it seems that android doesnt read those properties. I also put this info into Settings section of OS (via GUI). ANy ideas? Thx.

    Properties props = System.getProperties();
    props.put("http.proxyHost", "190.128.1.69");
    props.put("http.proxyPort", "80"); 

回答1:

Two ways to do it.

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);