栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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足够好,对我来说也足够好。

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483634.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号