HttpURLConnection-“ https:”和“ http:”

HttpURLConnection-“ https:”和“ http:”,第1张

HttpURLConnection-“ https://”和“ http://”

除非您使用user2558882的想法,否则除非有其他工具可以为您提供网站图标,否则您将必须同时检查http和https网址。没有其他方法可以做到这一点。这是使用网络的困难之一。

也许以不同的方式看待您的代码并将您尝试做的事情分解为更小,更易于管理的部分会更好一些?

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.depreStream(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();}

顺便说一句,关注一个或两个连接需要花费更多时间,然后编写代码以发出两个请求。我几乎保证Google会根据需要发出两个请求。如果它对Google足够好,对我来说也足够好。

最后,如果您开始发现发出两个请求确实花费了太多时间,则可以采取一些措施来提高性能。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5475774.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存