[ruby on rails]Net::HTTP使用

  • 一,二不支持请求头设置(header取ruby默认值),只能用于基本的请求,不支持持久连接,如果您执行许多HTTP请求,则不推荐它们;
  • 三,四可以设置请求头;
  • NET::HTTP不能处理重定向和404 ;不支持会话保持

一、 基本GET请求

require 'net/http'
uri = URI('http://httpbin.org/get')
res = Net::HTTP.get_response(uri)
res.code => '200'
res.body => body string
JSON.parse(res.body)

# 带参数的GET请求
uri = URI('http://httpbin.org/get?name=zhaofan&age=23')
res = Net::HTTP.get_response(uri)
res.body

# 如果我们想要在URL查询字符串传递数据,通常我们会通过httpbin.org/get?key=val方式传递
# URI模块允许使用 encode_www_form 方法,将一个HASH来进行转化,例子如下:
uri = URI('http://httpbin.org/get?')
data = {name:'zhaofan', age:23}
# hash转化成x-www-form-urlencoded
# data.to_param
# data.to_query 
uri.query = URI.encode_www_form(data)
res = Net::HTTP.get_response(uri)
res.body

二.基本post请求post_form(header默认为application/x-www-form-urlencoded)

uri = URI('http://httpbin.org/post')
params = {name:'zhaofan', age:23}
res = Net::HTTP.post_form(uri, params)
res.body

三.get固定用法

url = URI('http://httpbin.org/get')
params = {'name': 'test'}
url.query = URI.encode_www_form(params)
http = Net::HTTP.new(url.host, url.port)
header = {'user-agent': 'Chrome/61.0.3163.100'}
res = http.get(url, header)
res.body

四.post固定用法

  • application/json
url = URI('http://httpbin.org/post')
http = Net::HTTP.new(url.host, url.port)
params = {'code':1, 'sex':'男', 'id':1900, 'name': 'test'}.to_json
header = {'content-type': 'application/json'}
res = http.post(url, params, header)
res.body
  • application/x-www-form-urlencoded
url = URI('http://httpbin.org/post')
http = Net::HTTP.new(url.host, url.port)
params = URI.encode_www_form({'name':'test'})
header = {'content-type': 'application/x-www-form-urlencoded'}
res = http.post(url, params, header)
res.body
  • multipart/form-data 上传文件
url = URI('http://httpbin.org/post')
http = Net::HTTP.new(url.host, url.port)
# 设置请求参数
params = [
    ['name', 'test'],
    ['image', open('test.jpg'), { filename: 'test.jpg'}]
]
request = Net::HTTP::Post.new(url.path)
request.set_form(params, 'multipart/form-data')
res = http.request(request)
puts res.body

PUT用法

url = URI.parse(@url)
request = Net::HTTP::Put.new(url, 'Content-Type' => content_type)
request.body = data
response = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') {|http| http.request(request)}
url = URI.parse(@url)
request = Net::HTTP::Put.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Cache-Control"] = 'no-cache'
request["Postman-Token"] = '9e3622cd-fda5-458d-2521-325849546ef7'
request.body = "{\"name\": \"anonymous\", \"password\": \"obfuscated\"}"
response = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') {|http| http.request(request)}

五.response响应内容

uri = URI('http://www.baidu.com')
response = Net::HTTP.get_response(uri)
puts "http版本:#{response.http_version}" #=> 1.1
puts "响应代码: #{response.code}" #=> 200
puts "响应信息:#{response.message}" #=> ok
puts "uri:#{response.uri}" #=> http://www.baidu.com
puts "解码信息:#{response.decode_content}" #=> true
puts response.body #=> 响应体
response.header.each do |k,v|
  puts "#{k}:#{v}"
end #=> 响应头

六.发送https请求

http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

七.使用代理

proxy_ip = '47.115.5.19'
proxy_port = 16816

page_url = "https://dev.kuaidaili.com/testproxy"
uri = URI(page_url)

proxy = Net::HTTP::Proxy(proxy_ip, proxy_port)

# 创建新的请求对象 
req = Net::HTTP::Get.new(uri)

# 设置User-Agent
req['User-Agent'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
req['Accept-Encoding'] = 'gzip'  # 使用gzip压缩传输数据让访问更快

# 使用代理发起请求, 若访问的是http网页, 请将use_ssl设为false
res = proxy.start(uri.hostname, uri.port, :use_ssl => true) do |http|
    http.request(req)
end

八. Open使用代理

require "open-uri"
html = Nokogiri::HTML(open(address, 'User-Agent' => agent, 'Cookie' => cookie, :proxy => "http://127.0.0.1:7890").read)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值