wget や ブラウザが使用できない際に、rubyで簡易的にページを取得
レスポンスヘッダ等の情報が知りたい場合に使用
http_get.rb
#!/bin/env ruby
require 'net/http'
if ARGV.size == 0
puts "usage: httpget.rb <url>"
exit 1
end
url=ARGV[0]
schema, tmp=url.split("://")
pos=tmp.index("/")
if pos.nil?
host = tmp
else
host = tmp[0, pos]
path = tmp[pos..-1]
end
path = "/" if path.nil?
http = Net::HTTP.new(host, 80);
request_header = {}
request_header["User-Agent"] = "hogehoge"
request_header["Connection"] = "Keep-Alive"
response = http.get(path, request_header)
puts "STATUS CODE: #{response.code}"
response.each { |k,v|
puts "#{k} : #{v}"
}
puts "outputing res_body.dat"
File.open("res_body.dat", "w") { |file|
file.write response.body
}
[user@hoge script]$ ruby ./http_get.rb http://www.google.co.jp
STATUS CODE: 200
server : gws
vary : Accept-Encoding
expires : -1
accept-ranges : none
set-cookie : PREF=ID=1111111111111111:FF=0:TM=1444185579:LM=1444185579:V=1:S=1Ld2B6b7S-uc3Oym; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.co.jp, NID=72=mOgQBVn1blEgYNHYcNUahz-1hm1zaUxBXLvvBgzrEKY7YuZ98J08Ik0iPzN9Y3NtamsaR5lxybhboylr8GH4osO_Mh8T0AL4WTVyAeFmUUuE65Vd4goG9f1r0SqCwRqdvL_ZdQ; expires=Thu, 07-Apr-2016 02:39:39 GMT; path=/; domain=.google.co.jp; HttpOnly
p3p : CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
x-frame-options : SAMEORIGIN
transfer-encoding : chunked
date : Wed, 07 Oct 2015 02:39:39 GMT
cache-control : private, max-age=0
content-type : text/html; charset=Shift_JIS
x-xss-protection : 1; mode=block
outputing res_body.dat