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. piranha配置

    典型的高可用负载均衡 1)lvs + ldirectord + heartbeat lvs(ipvsadmin) 调度器,将用户请求分发到后端真实服务器,不负责健康检查 ldirectord 服务监控 ...

  2. 【NLP_Stanford课堂】正则表达式

    或者 [Ww]oods,方括号里的是或的关系,符合其一即被提出.用来匹配单个字符 [A-Z]:表示所有的大写字母之一 [a-z]:表示所有的小写字母之一 [0-9]:表示所有的0-9的数字之一 否定: ...

  3. python字符串反转 高阶函数 @property与sorted(八)

    (1)字符串反转 1倒序输出 s = 'abcde' print(s[::-1]) #输出: 'edcba' 2 列表reverse()操作 s = 'abcde' lt = list(s) lt.r ...

  4. 二、安装桌面——Linux学习笔记

    安装桌面并不是商业化用的,只是为了熟悉Linux文档结构而已. 这个比较简单 1.输入安装桌面命令 # yum -y groups install "GNOME Desktop" ...

  5. c#利用三层架构做一个简单的登录窗体

    就个人而言,三层架构有点难理解,不知道该如何下手,各层与各层之间怎么调用 最近一直在研究三层架构,经过网上学习与多方打听写一下自己的心得.有不足之处,可以评论和私聊探讨 言归正传: 三层架构(3-ti ...

  6. Linux(Ubuntu) 常用命令

    玩儿转Linux:终端命令用法精选 最近再一次拾起了Ubuntu,为了更好的玩儿转Linux,专门到网上搜到的这些常用的终端命令,根据命令使用类别的不同分为了9个大类,都在下面一一列举了出来,个人觉得 ...

  7. x64 分页机制——虚拟地址到物理地址寻址

    原博客:http://www.cnblogs.com/lanrenxinxin/p/4735027.html 详细的理论讲解都在上面 下面说的是通过windbg手动进行寻址,深入理解 x64: 实践: ...

  8. oozie说明(本文参考多处,自己留看)

    Oozie概述: Oozie是一个基于Hadoop工作流引擎,也可以称为调度器,它以xml的形式写调度流程,可以调度mr,pig,hive,shell,jar,spark等等.在实际工作中,遇到对数据 ...

  9. python图片黑白化

    #!/usr/bin/env python #-*- coding:utf-8 -*- from PIL import Image im = Image.open(r"C:\Users\wa ...

  10. oracle spatial下对wkt字符串操作遇到srid的解决方案

    <span style="font-size:18px;">select fid from vgnss where SDO_WITHIN_DISTANCE(geom,  ...