结合使用Webview和代理进行身份验证

编程入门 行业动态 更新时间:2024-10-22 17:30:11
本文介绍了结合使用Webview和代理进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在WebView中加载一些在一个国家/地区不可用的网址,因此我尝试将代理与WebView一起使用.我在SO( stackoverflow/a/18453384/7478869 )上找到了一个解决方案,它无需身份验证即可与代理一起使用.但是我需要使用用户名和密码设置代理.

I need to load some url in WebView that is unavailable in one country, so I tried to use a proxy with WebView. I've found one solution on SO (stackoverflow/a/18453384/7478869), and it works with proxy without authentication. But I need to set the proxy with user and password.

有些方法没有帮助:

1)加载带有标题的网址

1) Load url with headers

class ProxyAuthWebViewClient extends WebViewClient { String proxyUserName; String proxyPassword; public ProxyAuthWebViewClient(String proxyUserName, String proxyPassword){ this.proxyUserName = proxyUserName; this.proxyPassword = proxyPassword; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { loadUrl(view, url, proxyUserName, proxyPassword); return true ; } } public void loadUrl(WebView view, String url, String proxyUserName, String proxyPassword){ UsernamePasswordCredentials creds= new UsernamePasswordCredentials(proxyUserName, proxyPassword); Header credHeader = BasicScheme.authenticate(creds, "UTF-8", true); Map<String, String> header = new HashMap<String, String>(); header.put(credHeader.getName(), credHeader.getValue()); view.loadUrl(url, header); }

2)在setProxy方法中添加密码和用户(以下完整代码):

2) Add password and user in setProxy method (full code below):

Authenticator.setDefault( new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( user, password.toCharArray()); } } ); System.setProperty("http.proxyUser", user); System.setProperty("http.proxyPassword", password); System.setProperty("https.proxyUser", user); System.setProperty("https.proxyPassword", password );

但是我仍然收到此错误

[WARNING:http_network_transaction.cc(339)] Blocked proxy response with status 407 to CONNECT request for example:443

在WebView中:ERR_TUNNEL_CONNECTION_FAILED

And in WebView: ERR_TUNNEL_CONNECTION_FAILED

完整代码:

public class MainActivity extends AppCompatActivity { public static final String LOG_TAG = "Main"; WebView webview; String applicationClassName="android.app.Application"; String user = "web"; String password = "password"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webview = findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); String databasePath = webview.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); webview.getSettings().setDatabaseEnabled(true); webview.getSettings().setDatabasePath(databasePath); webview.getSettings().setAppCacheMaxSize(5 * 1048576); webview.getSettings().setAppCachePath(databasePath); webview.getSettings().setAppCacheEnabled(true); webview.getSettings().setLoadWithOverviewMode(true); webview.getSettings().setDomStorageEnabled(true); webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); setProxy(webview, "85.10.195.100", 443, applicationClassName, user, password); webview.setWebViewClient(new ProxyAuthWebViewClient(user,password)); loadUrl(webview,"example",user,password); } class ProxyAuthWebViewClient extends WebViewClient { String proxyUserName; String proxyPassword; public ProxyAuthWebViewClient(String proxyUserName, String proxyPassword){ this.proxyUserName = proxyUserName; this.proxyPassword = proxyPassword; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { loadUrl(view, url, proxyUserName, proxyPassword); return true ; } } public void loadUrl(WebView view, String url, String proxyUserName, String proxyPassword){ UsernamePasswordCredentials creds= new UsernamePasswordCredentials(proxyUserName, proxyPassword); Header credHeader = BasicScheme.authenticate(creds, "UTF-8", true); Map<String, String> header = new HashMap<String, String>(); header.put(credHeader.getName(), credHeader.getValue()); view.loadUrl(url, header); } public static boolean setProxy(WebView webview, String host, int port, String applicationClassName, String user, String password) { return setProxyKKPlus(webview, host, port, user, password, applicationClassName); } // from stackoverflow/questions/19979578/android-webview-set-proxy-programatically-kitkat @SuppressLint("NewApi") @SuppressWarnings("all") private static boolean setProxyKKPlus(WebView webView, String host, int port, final String user, final String password, String applicationClassName) { Log.d(LOG_TAG, "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 + ""); Authenticator.setDefault( new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( user, password.toCharArray()); } } ); System.setProperty("http.proxyUser", user); System.setProperty("http.proxyPassword", password); System.setProperty("https.proxyUser", user); System.setProperty("https.proxyPassword", password ); try { Class applictionCls = Class.forName(applicationClassName); Field loadedApkField = applictionCls.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(LOG_TAG, "Setting proxy with >= 4.4 API successful!"); return true; } catch (ClassNotFoundException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } catch (NoSuchFieldException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } catch (NoSuchMethodException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } catch (InvocationTargetException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(LOG_TAG, e.getMessage()); Log.v(LOG_TAG, exceptionAsString); } return false; } }

如何在WebView中正确使用具有身份验证的代理?

How to use a proxy with authentication in WebView properly?

推荐答案

好,所以我找到了答案.需要在 WebViewClient

Ok, so I Found the answer. Need to add onReceivedHttpAuthRequest in WebViewClient

@Override public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { handler.proceed(user,password); }

更多推荐

结合使用Webview和代理进行身份验证

本文发布于:2023-11-03 16:19:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Webview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!