简体   繁体   English

创建带有白名单的自定义浏览器 android

[英]Creating a custom browser with whitelist android

I'm trying to make a simple custom browser that contains a whitelist of allowed websites.我正在尝试制作一个简单的自定义浏览器,其中包含允许网站的白名单。 My browser is just a webview with an address bar.我的浏览器只是一个带有地址栏的 webview。 When comparing the requested website to the whitelist it works fine if the website is just www.yahoo.com.将请求的网站与白名单进行比较时,如果该网站只是 www.yahoo.com,则可以正常工作。 If www.yahoo.com is on the whitelist it will navigate to the website.如果 www.yahoo.com 在白名单上,它将导航到该网站。 The problem I am running into is when I get to www.yahoo.com it is their mobile site, "m.yahoo.com" and will not navigate to any of their links, because URL.getHostName() is m.yahoo.com which does not equal www.yahoo.com that is on the whitelist.我遇到的问题是当我访问 www.yahoo.com 时,它是他们的移动网站“m.yahoo.com”,并且不会导航到他们的任何链接,因为 URL.getHostName() 是 m.yahoo。 com 不等于白名单上的 www.yahoo.com。 Right now I am just using URL.getHostName() to compare to the whitelist.现在我只是使用 URL.getHostName() 来比较白名单。 Is there a better way to compare the requested website to the whitelist?有没有更好的方法将请求的网站与白名单进行比较?

There's a few ways you can approach this problem.有几种方法可以解决这个问题。

1) You could simply add the m.yahoo.com links to your whitelist. 1) 您可以简单地将m.yahoo.com链接添加到您的白名单中。 That might be the simplest solution.这可能是最简单的解决方案。

2) Depending on if it's appropriate, you could change the user-agent string of your browser to identify itself as a desktop browser. 2) 根据是否合适,您可以更改浏览器的用户代理字符串以将自己标识为桌面浏览器。 Briefly, you call getSettings() on the WebView and call its setUserAgentString() method, passing in the user agent string from a popular desktop web browser.简而言之,您在 WebView 上调用getSettings()并调用其setUserAgentString()方法,从流行的桌面 web 浏览器传入用户代理字符串。

3) You could write a little Java code to parse the URL.getHostName() to strip the hostname down to only the top-level domain name, (eg " m.yahoo.com " -> " yahoo.com "), and then compare against the whitelist. 3) You could write a little Java code to parse the URL.getHostName() to strip the hostname down to only the top-level domain name, (eg " m.yahoo.com " -> " yahoo.com "), and然后与白名单进行比较。

4) You could do your whitelist matching on hostname patterns, rather than simple strings. 4)您可以对主机名模式进行白名单匹配,而不是简单的字符串。 Make each entry in your whitelist a regular expression that matches a variety of hosts.使白名单中的每个条目成为匹配各种主机的正则表达式。 Then you just match each entry in your whitelist against URL.getHostName() .然后,您只需将白名单中的每个条目与URL.getHostName()进行匹配。 If you're new to regular expressions, or regular expressions in Java, there's plenty of tutorials available , and help is available here on Stack Overflow.如果您不熟悉正则表达式或 Java 中的正则表达式,这里有很多可用的教程,并且可以在 Stack Overflow 上获得帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM