[转载]利用ruby的Net::HTTP发起http请求并对返回包进行简单的校验

一、先上一段代码:
#encoding = utf-8   
require 'net/http'  
  
Net::HTTP.start('tuan.qq.com', 80) {|http|   
    response = http.get('/')           #获取返回包所有信息   
    #response = http.head('/')         #仅仅获取返回头信息   
    head_hash = response.to_hash   
    head_hash.keys.each {|key|    
        p key.to_s + ':' + head_hash[key].to_s   
    }   
}  

#encoding = utf-8  require 'net/http'    Net::HTTP.start('tuan.qq.com', 80) {|http|      response = http.get('/')           #获取返回包所有信息      #response = http.head('/')         #仅仅获取返回头信息      head_hash = response.to_hash      head_hash.keys.each {|key|           p key.to_s + ':' + head_hash[key].to_s      }  }

对于上面的代码:
start方法指定了访问的host和端口
get后面指定的参数即为CGI去除host的部分,返回response的hash
head方法是仅仅获取response返回头信息

运行上面的代码,我们可以得到如下结果:


"vary:Accept-Encoding"  
"server:Apache"  
"connection:close"  
"date:Thu, 07 Jun 2012 08:05:46 GMT"  
"cache-control:max-age=0"  
"content-type:text/html"  
"transfer-encoding:chunked"  
"expires:Thu, 07 Jun 2012 08:05:46 GMT"  
"set-cookie:vip_city_tuan_city=deleted; expires=Wed......  

"vary:Accept-Encoding"  "server:Apache"  "connection:close"  "date:Thu, 07 Jun 2012 08:05:46 GMT"  "cache-control:max-age=0"  "content-type:text/html"  "transfer-encoding:chunked"  "expires:Thu, 07 Jun 2012 08:05:46 GMT"  "set-cookie:vip_city_tuan_city=deleted; expires=Wed......

这就是返回包的头信息!因为这个response是一个hash,所以我们可以获取里面每一项的信息,如下:


p response['server']         #  Apache   
p response['content-type']   #  text/html  

p response['server']         #  Apache  p response['content-type']   #  text/html

二、也可以通过以下方法,不指定host,直接利用url发送请求,获取返回包


response = Net::HTTP.get_response(URI('http://tuan.qq.com'))  

response = Net::HTTP.get_response(URI('http://tuan.qq.com'))

获取response的状态信息: 


response = Net::HTTP.get_response(URI('http://www.baidu.com'))   
p response.code             #"200"   
p response.content_length   #8023   
p response.message          #"OK"   
p response.body             #获取body信息        
#"<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"><title>\260\331\266\310\3..."  

response = Net::HTTP.get_response(URI('http://www.baidu.com'))  p response.code             #"200"  p response.content_length   #8023  p response.message          #"OK"  p response.body             #获取body信息       #"<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"><title>\260\331\266\310\3..."

利用上面的几个简单方法,就可以实现简单的cgi自动化!

也可以批量扫描页面链接,检查其状态!

原文地址:https://www.cnblogs.com/zhangfei/p/2590375.html