ruby中的NET::HTTP;这里暂时先列出几个固定用法:

其中一,二不支持请求头设置(header取ruby默认值),只能用于基本的请求,不支持持久连接,如果您执行许多HTTP请求,则不推荐它们;三,四可以设置请求头;

NET::HTTP不能处理重定向和404 ;不支持会话保持

一. 基本GET请求get_response

require 'net/http'

# 基本GET请求
require 'net/http'
uri = URI('http://httpbin.org/get')
response = Net::HTTP.get_response(uri)
puts response.body # 带参数的GET请求
require 'net/http'
uri = URI('http://httpbin.org/get?name=zhaofan&age=23')
response = Net::HTTP.get_response(uri)
puts response.body # 如果我们想要在URL查询字符串传递数据,通常我们会通过httpbin.org/get?key=val方式传递
# URI模块允许使用 encode_www_form 方法,将一个HASH来进行转化,例子如下:
require 'net/http'
uri = URI('http://httpbin.org/get?')
data = {name:'zhaofan', age:23}
uri.query = URI.encode_www_form(data)
response = Net::HTTP.get_response(uri)
puts response.body

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

require 'net/http'

# 基本POST请求
# 通过在发送post请求时添加一个params参数,这个params参数可以通过字典构造成;
# 该方法底层和get_response一样都是调用了response方法
uri = URI('http://httpbin.org/post')
params = {name:'zhaofan', age:23}
res = Net::HTTP.post_form(uri, params)
puts res.body

三.get固定用法

require 'net/http'

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'} response = http.get(url, header)
puts response.body

四.post固定用法

  • application/json

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

五.response响应内容

require 'net/http'
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请求

require 'net/https'
# 设置https
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

ruby net/http模块使用的更多相关文章

  1. Ruby类,模块1

    类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...

  2. ruby中的模块

    什么是模块 模块(module)是Ruby特有的功能之一.类用来表现具有数据与行为(程序)的"东西", 而模块大致来说,则是只有程序部分的集合体.类与模块最大的不同在于: 1.模块 ...

  3. Ruby中Enumerable模块的一些实用方法

    我在查看 Array 类和 Hash 类的祖先链的时候都发现了 Enumerable,说明这两个类都mixin了Enumerable模块.Enumerable模块为集合型类提供了遍历.检索.排序等方法 ...

  4. ruby 可枚举模块Enumerable

    Enumerable模块提供了遍历,搜索,比较,排序等方法.如果我们自定义的类需要实现这些方法,必须实现一个each方法.如果需要使用max,min,sort等方法,因为这些方法是集合的元素之间的排序 ...

  5. Ruby On Rails 常用的精品Gem汇总

    首先需要注明一点,本文是原创的并不是从其它地方转载.所有的数据是我从 GitHub 和 RubyGems 上码下来的,数据的截取时间就是本文的发布日期. RubyGems 的下载量可以看到在用这个 g ...

  6. 【转】教你Ruby快速入门

    转自:http://developer.51cto.com/art/200703/41243.htm 介绍 这是一个短小的Ruby入门,完全读完只需20分钟.这里假设读者已经安装了Ruby,如果你没有 ...

  7. 《ruby编程语言》笔记 1

    赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...

  8. ruby klb.rb irb

    1.字符串格式化 Python "%s=%s" % (k, v) 在阅读 Python 字符串格式化的时候,视线先看到字符串的 %s 字样,但是不知道这指的是什么,然后看后面的变量 ...

  9. Ruby:多线程队列(Queue)下载博客文章到本地

    Ruby:多线程下载博客文章到本地的完整代码 #encoding:utf-8 require 'net/http' require 'thread' require 'open-uri' requir ...

随机推荐

  1. spark学习地址

    http://blog.sina.com.cn/s/blog_64d9a6ef0101ghvs.html http://blog.sina.com.cn/s/blog_49cd89710102v3b1 ...

  2. Mantis去掉登录界面的“注册一个新账号”链接

    Mantis1.1.2主界面提供了新账号注册功能,但是只能注册默认权限的帐号.不是很实用,那就干脆去掉吧. (1) 打开Mantis目录下的login_page.php和lost_pwd_page.p ...

  3. supervisor运行virtualenv环境下的nagios-api

    supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...

  4. HTTP Strict Transport Security

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. 作用 一个网站接受一个HTTP的 ...

  5. matlab解方程

    [x1,y1,x2,y2]=solve('x1^2 + y1^2=1','x2^2-8*x2 +y2^2 +15=0','x1*x2 + y1 * y2=1','x1 + x2 =a','x1','y ...

  6. Jmeter入门19 保存测试结果(或从文件读取结果)

    以聚合报告为例,其他监听器有write results to file的类似. 首先 为了避免每次保存的测试报告被覆盖,我们在testplan下添加两个参数:项目名和当前时间(毫秒级) 其次 添加聚合 ...

  7. iTunes备份路径,iTunes默认备份路径,iTunes修改备份路径

    1:当前iTunes版本: 2:帮助给出的答复: 3:修改的操作界面: 实际文件夹路径:

  8. 【洛谷P4342】[IOI1998]Polygon

    Polygon 比较裸的环形DP(也可以说是区间DP) 将环拆成链,复制到后面,做区间DP即可 #include<iostream> #include<cstdio> usin ...

  9. Promise面试题

    题目一 const promise = new Promise((resolve, reject) => { console.log(1); resolve(); console.log(2); ...

  10. HTML中id和class选择器

    <1>.id和class的区别? id相当于人的身份证不可以重复 class相当于人的名称可以重复 一个HTML标签只能绑定一个id名称 一个HTML标签可以绑定多个class名称 < ...