open-uri库是ruby的标准附加库中所带的,路径是:lib/oepn-uri.rb
引用了该附加库后,就可以像打开普通文件那样打开一个http/ftp的uri的资源。
有区别的地方在这里:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
还有一点要注意,就是open-uri里面的open方法会自动对302 redirection重定向地址进行追踪访问,而Net::HTTP#get方法则不会自动追踪302重定向地址。
该函数最后返回的值,是传入的代码块最后的值。 可以这样获得uri的内容
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第二句的open的原因是:open方法返回的流,是已经关闭了的tempfile,所以要先open打开流,返回打开的流,再调用read读取。