我一直在尝试设置代理和使用从androidwebview,但当加载网站总是返回 net::error_timed_out
我使用过netcipher和normal system.setproperty(“http.proxyhost”,ip);方法,它总是返回true“表示连接成功”,但当我尝试运行 webView.loadUrl(link);
它回来了 net::error_timed_out
有时链接我试图加载,加载成功
我使用的功能:
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private boolean setProxy(String host, String port) {
Log.d("Debug", "Setting proxy with >= 4.4 API.");
Context appContext = webView.getContext().getApplicationContext();
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port + "");
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", port + "");
try {
Field loadedApkField = appContext.getClass().getField("mLoadedApk");
loadedApkField.setAccessible(true);
Object loadedApk = loadedApkField.get(appContext);
Class loadedApkCls = Class.forName("android.app.LoadedApk");
Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
receiversField.setAccessible(true);
ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
for (Object receiverMap : receivers.values()) {
for (Object rec : ((ArrayMap) receiverMap).keySet()) {
Class clazz = rec.getClass();
if (clazz.getName().contains("ProxyChangeListener")) {
Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
onReceiveMethod.invoke(rec, appContext, intent);
}
}
}
Log.d("Debug", "Setting proxy with >= 4.4 API successful!");
return true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return false;
} catch (InvocationTargetException e) {
e.printStackTrace();
return false;
} catch (NoSuchFieldException e) {
e.printStackTrace();
return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
return false;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
连接功能:
WebViewActivity.this.runOnUiThread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void run() {
try {
// boolean worked = WebkitProxy.setProxy(MainActivity.class.getName(), getApplicationContext(), webView, proxyDomain, Integer.parseInt(proxyPort));
boolean worked = setProxy(proxyIP,proxyPort);
Log.d("Debug","IP = "+proxyIP+" ProxyPort = "+proxyPort + " worked= "+worked);
webView.setWebChromeClient(new WebChromeClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl(link);
} catch (Exception e) {
Log.d("Debug", "Could not start WebkitProxy " + e.getMessage() + " Number : "+number);
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WebViewActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
暂无答案!
目前还没有任何答案,快来回答吧!