问题
I want to browse some website with proxy that i already have (with apps that i want to make of course). i want to change it programmatically (without go to settings or other 3rd apps included).
here is what i've done :
public class WebViewActivity extends Activity {
WebView web;
String PROXY_IP = "MyProxy";
int PROXY_PORT = MyPort;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
web = (WebView) findViewById(R.id.webView1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("MyURL");
if (PROXY_IP != null) {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().getCredentials(new AuthScope(PROXY_IP, PROXY_PORT));
new UsernamePasswordCredentials("username","passsword");
HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);
}
}
I use webview for this. i already tried using phonegap and just editing the asset. but i'm confused about some php code. so i decided using webview instead.
The Problem is how to make my webview using my proxy.
Note: i'm using GB 2.3
回答1:
Have you tried Android Proxy Library ? https://github.com/shouldit/android-proxy-library
It's maybe a solution for you
回答2:
Problem Solved. in webview onCreate
i have to call url. like this web = callURL();
and to load url use loadDatawithBaseUrl. after that you have to make a public class callURL(){
. set the proxy in there. like this
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost proxy = new HttpHost("your host", your proxy); //proxy that i need
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
httpClient.getCredentialsProvider().setCredentials(
new AuthScope("your host", your proxy),
new UsernamePasswordCredentials(
"your username", "your password"));
and then in httpget. call your url too. thats it. good luck
回答3:
Try the following method. I dont know this is whether correct or not. Just try.
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,new
NTCredentials(user, pass, null, null));
来源:https://stackoverflow.com/questions/16391724/android-proxy-setting