ruby进行http请求头设置及获取

来源:互联网 发布:mia wasikowska 知乎 编辑:程序博客网 时间:2024/05/23 15:41
#encoding: utf-8#http://www.ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html   ##官方样例require 'net/http'http = Net::HTTP.new('www.baidu.com', 80)http.use_ssl = falsepath = '/'resp, data = http.get(path)cookies = resp.response['set-cookie'].split(', ')  #获取cookiesputs cookiesheaders = {   ##定义http请求头信息  'Cookie' => cookies[0].split('; ')[0],  'Referer' => 'http://qa.dangdang.com',  'Content-Type' => 'application/x-www-form-urlencoded'}resp, data = http.get(path, headers)puts 'Code = ' + resp.code    ##请求状态码puts 'Message = ' + resp.message  resp.each {|key, val| puts key + ' = ' + val}  ##遍历所有http响应头




原创粉丝点击