用Ruby做Get网络请求
编程技术  /  houtizong 发布于 3年前   60
>>require "open-uri">>open("http://www.cnblog.org/blog/atom.xml")
>> open("http://www.cnblog.org/blog/atom.xml")Errno::ETIMEDOUT: Connection timed out - connect(2) from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `initialize' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `open' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `connect' from /usr/local/bin/rubyee/lib/ruby/1.8/timeout.rb:53:in `timeout' from /usr/local/bin/rubyee/lib/ruby/1.8/timeout.rb:93:in `timeout' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `connect' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:553:in `do_start' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:542:in `start' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:242:in `open_http' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:616:in `buffer_open' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:164:in `open_loop' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:162:in `catch' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:162:in `open_loop' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:132:in `open_uri' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:518:in `open' from /usr/local/bin/rubyee/lib/ruby/1.8/open-uri.rb:30:in `open' from (irb):6>>
class HandleGetRequest # 对url发起get请求 require 'net/http' def self.get_response(url) begin url_str = URI.parse(url) site = Net::HTTP.new(url_str.host, url_str.port) site.open_timeout = 20 site.read_timeout = 20 path = url_str.query.blank? ? url_str.path : url_str.path+"?"+url_str.query return site.get2(path,{'accept'=>'text/html','user-agent'=>'Mozilla/5.0'}) rescue Exception => ex p ex end endend
>> HandleGetRequest.get_response("http://www.iteye.com/topic/431217")=> #<Net::HTTPOK 200 OK readbody=true>
>> HandleGetRequest.get_response("http://www.google.com.hk")#<ArgumentError: HTTP request path is empty>=> nil>> HandleGetRequest.get_response("http://www.google.com.hk/")=> #<Net::HTTPOK 200 OK readbody=true>
>> HandleGetRequest.get_response("http://www.cnblog.org/blog/atom.xml")#<Timeout::Error: execution expired>Timeout::Error: execution expired from /usr/local/bin/rubyee/lib/ruby/1.8/timeout.rb:60:in `open' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `connect' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:560:in `connect' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:553:in `do_start' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:542:in `start' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:1035:in `request' from /usr/local/bin/rubyee/lib/ruby/1.8/net/http.rb:948:in `get2' from /home/chengliwen/chengliwen/deploy/pin-macro-tmp/lib/handle_get_request.rb:30:in `get_response' from (irb):1
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接