使用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.