我正在尝试获取用户输入的URL的网站图标,例如
_url = "google.com";
我使用 HttpUrlConnection 从主机 URL 的 /favicon.ico
扩展获取网站图标的位图。
String faviconString = Uri.parse(_url).getHost() + "/favicon.ico";
URL faviconUrl = null;
Bitmap favicon = null;
try
{
faviconString = "http://" + faviconString;
faviconUrl = new URL(faviconString);
HttpURLConnection connection = (HttpURLConnection) faviconUrl.openConnection();
connection.setDoInput(true);
connection.connect();
favicon = BitmapFactory.decodeStream(connection.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
return favicon;
然而,由于用户可能不会指定http://
或https://
,因此我必须自己添加。我遇到的问题是,如果在URL前面添加http://
,一切都会正常工作,但对于https://
,有些站点将返回favicon,而另一些站点则只会给我null。我如何找出哪个页面使用了https
?我是否应该为每种情况都添加http://
?是否有任何网站严格限制使用https
并返回null以供使用http
?
<link rel="shortcut icon" href="/a/different/path.ico" type="image/x-icon" />
。 - EmielWebIconDatabase
和WebView.getFavicon()
是理想的选择,但是WebIconDatabase
已经被弃用了。 - Jason HuUri.parse(_url).getHost() + "/favicon.ico"
非常不现实。但是,如果您坚持只获取此路径,可以进行HEAD请求以了解有关资源的详细信息,而无需获取数据。在此处检查:http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 - Sherif elKhatib