使用Ruby,获取给定URL内容类型的最有效方法是什么? [英] Using Ruby, what is the most efficient way to get the content type of a given URL?

查看:80
本文介绍了使用Ruby,获取给定URL内容类型的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ruby获取给定URL的内容类型的最有效方法是什么?

What is the most efficient way to get the content-type of a given URL using Ruby?

推荐答案

这是什么如果我想要简单的代码,我会这样做:

This is what I'd do if I want simple code:

require 'open-uri'
str = open('http://example.com')
str.content_type #=> "text/html"

最大的好处是重定向之后。

The big advantage is it follows redirects.

如果要检查一堆URL,您可能要在处理完之后在句柄上调用 close 找到了想要的东西。

If you're checking a bunch of URLs you might want to call close on the handles after you've found what you want.

这篇关于使用Ruby,获取给定URL内容类型的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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