HttpURLConnection - " https://"与“http://”相对 [英] HttpURLConnection - "https://" vs. "http://"

查看:132
本文介绍了HttpURLConnection - " https://"与“http://”相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户输入的网址的图标,例如

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

_url = "google.com";

我使用HttpUrlConnection从 / favicon获取favicon的位图。 ico 来自主机网址的扩展名。

I use HttpUrlConnection to get the Bitmap of the favicon from the /favicon.ico extension from the host url.

        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:// ,我必须自己添加。我遇到的问题是,如果我在网址前添加 http:// ,一切都会正常,但对于 https: // ,有些网站会返回favicon,其他网站只会给我null。如何找出哪个页面使用 https ?我应该为每个案例添加 http:// 吗?是否有任何网站严格限制 https 并使用 http 返回null?

However, since the user probably won't specify http:// or https://, I would have to add it myself. The problem I'm having is that, if I add http:// in front of the url, everything would work fine, but for https://, some sites would return the favicon, others would just give me null. How do I find out which page uses https? Should I just add http:// for every case? Are there any websites that restricts to strictly https and would return null for using http?

推荐答案

除非你使用user2558882的想法或者有在野外的一些其他工具将为您获取网站图标,您将不得不检查http和https网址。没有其他办法可以做到这一点。这是使用网络难度的一部分。

Unless you use user2558882's idea or there is some other tool out in the wild that will just get a websites favicon for you, you're going to have to check both the http and https urls. There is no other way to do this. It is part of the difficulty of using the web.

或许以不同的方式查看您的代码并将您尝试做的事情分解为更小的更易于管理的部分将是更好吗?

Perhaps looking at your code differently and breaking down what you're trying to do into smaller more manageable parts would be a bit better?

public void getFavicon(String host) {

    URL httpUrl = this.getHttpUrl(host + "/favicon.ico");

    Bitmap favicon = this.getBitmap(httpUrl);

    if (favicon == null) {

        URL httpsUrl = this.getHttpsUrl(host + "/favicon.ico");

        favicon = this.getBitmap(httpsUrl);
    }

    if (favicon == null) {

        throw new FaviconMissingException("Unable to find favicon for host: " + host);
    }

    return favicon;
}

public URL getHttpUrl(String uri) throws MalformedURLException {

    // There are better ways of building a url then string concationation.
    return new URL("http://" + uri);
}

public URL getHttpsUrl(String uri) throws MalformedURLException {

    // There are better ways of building a url then string concationation.
    return new URL("https://" + uri);
}

public Bitmap getBitmap(URL url) {

    InputStream inputStream = getInputStream(url);

    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

    return bitmap
}

public InputStream getInputStream(URL url) {

    // Please use a real connection library like HTTPClient here!
    // HttpClient will handle timeouts, redirects, and things like that for you.
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();

    return connection.getInputStream();
}

BTW,关注一两个连接需要花费更多时间然后编写代码提出两个请求。我几乎保证谷歌会根据需要提出两个请求。如果谷歌足够好对我来说已经足够了。

BTW, being concerned about one or two connections takes more time then writing the code to make two requests. I almost guarantee that google is making two requests as needed. And if it is good enough for google it is good enough for me.

最后,如果你开始看到发出两个请求真的花了太多时间,那么就做关于改善表现的事情。

Finally, if you start to see that making two requests is really taking too much time, then do something about improving the performance.

这篇关于HttpURLConnection - " https://"与“http://”相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆