HttpURLConnection - “https://” vs. “http://”

后端 未结 6 1814
无人及你
无人及你 2020-12-29 13:15

I\'m trying to get the favicon of the url the user enters, for example

_url = \"google.com\";

I use HttpUrlConnection to get the Bitmap of

6条回答
  •  忘掉有多难
    2020-12-29 13:51

    Note: I am not sure how helpful my answer would be.

    You can grab the favicon using google:

    http://www.google.com/s2/favicons?domain=stackoverflow.com
    

    returns:

    enter image description here

    You don't have to specify http or https.

     http://www.google.com/s2/favicons?domain=my.yorku.ca ===>> (https://my.yorku.ca)
    

    returns:

    enter image description here

    But this is not the actual favicon that https://my.yorku.ca uses. So, I guess google returns a default one for sites that do not provide access their favicons.

    InputStream is = null;
    
    String urlPrefix = "http://www.google.com/s2/favicons?domain=";
    
    String _url = "google.com";
    
    Bitmap favicon = null;
    
    try {
    
        is = (InputStream) new URL(urlPrefix + _url).getContent();
    
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    favicon = BitmapFactory.decodeStream(is);
    

    You can actually keep a copy of the default favicon and check if:

    if (defaultBitmap.sameAs(favicon)) {
        // favicon wasn't available
    }
    

提交回复
热议问题