简体   繁体   中英

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. When comparing the requested website to the whitelist it works fine if the website is just www.yahoo.com. If www.yahoo.com is on the whitelist it will navigate to the website. 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. Right now I am just using URL.getHostName() to compare to the whitelist. 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. 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. Briefly, you call getSettings() on the WebView and call its setUserAgentString() method, passing in the user agent string from a popular desktop web browser.

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.

4) You could do your whitelist matching on hostname patterns, rather than simple strings. 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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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