From: Takashi Hoshizawa Date: 2002-02-08T01:28:45+09:00 Subject: [ruby-list:33785] NET:HTTP get2 のエラー回避 星澤といいます。 NET:HTTP の get2 を使って、web robot に似たシステムを作成した ところ、かなりの数のサイトで、アクセス時に例外が発生しました。 下記のテストプログラムを実行すると、以下のような結果になります。 いずれも、IEやNetscapeであれば、正常に表示されます。 回避策について、御存知の方がいれば、ぜひ教えて下さい。 -- 星澤 隆 / Takashi Hoshizawa mail: t_hoshizawa@mbg.nifty.com テスト結果: (Aパターン) $ ruby http_test.rb http://www.interq.or.jp/tokyo/kk-kbys/ ERROR: Connection reset by peer $ ruby http_test.rb http://www.nagata-h.net/ ERROR: Connection reset by peer (Bパターン) $ ruby http_test.rb http://www.yatchan.com/ ERROR: execution expired $ ruby http_test.rb http://www.fujiwaramasashi.gr.jp/ ERROR: execution expired (Cパターン) $ ruby http_test.rb http://j-expert.com/ ERROR: execution expired (Bと同じだが、うまくgetできる時もある) テストプログラム (送信時、長い行が一部途中改行されてしまうかも知れません) #!/usr/local/bin/ruby # ----------------------------------------- # http_test.rb : net/http test program # # exsample: # $ ruby http_test.rb http://www.yahoo.co.jp # ----------------------------------------- require 'socket' require 'net/http' require 'kconv' def parse_http_url(url) host = port = abs_path = query = nil (host, port, abs_path, query), = url.scan(/^[hH][tT][tT][pP]:\/\/([^\/:]*)(?::(\d+))?(\/[^\?]*)?(.+)?/) if host if abs_path == nil abs_path = "/" end if port == nil port = 80 end end return host, port, abs_path, query end url = ARGV.shift host, port, abs_path, query = parse_http_url(url) header = Hash.new header['Accept'] = '*/*' header['User-Agent'] = 'test' begin http = Net::HTTP.new(host, port) Net::HTTP.version_1_2 http.open_timeout = 10 http.start http.read_timeout = 60 response = http.get2(abs_path, header) do |rf| rf.body do |s| puts Kconv.kconv(s, Kconv::AUTO, Kconv::EUC) end end content_type = response['content-type'] len = response['content-length'].to_i last_modified = response['last-modified'] puts "----" puts "url: #{url}" puts "response_code: #{response.code}" puts "content_type: #{content_type}" puts "length: #{len}" puts "last_modified: #{last_modified}" http.finish rescue puts "ERROR: #$!" if response puts "response_code: #{response.code}" end end