ruby net/http模块使用
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模块使用的更多相关文章
- Ruby类,模块1
类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...
- ruby中的模块
什么是模块 模块(module)是Ruby特有的功能之一.类用来表现具有数据与行为(程序)的"东西", 而模块大致来说,则是只有程序部分的集合体.类与模块最大的不同在于: 1.模块 ...
- Ruby中Enumerable模块的一些实用方法
我在查看 Array 类和 Hash 类的祖先链的时候都发现了 Enumerable,说明这两个类都mixin了Enumerable模块.Enumerable模块为集合型类提供了遍历.检索.排序等方法 ...
- ruby 可枚举模块Enumerable
Enumerable模块提供了遍历,搜索,比较,排序等方法.如果我们自定义的类需要实现这些方法,必须实现一个each方法.如果需要使用max,min,sort等方法,因为这些方法是集合的元素之间的排序 ...
- Ruby On Rails 常用的精品Gem汇总
首先需要注明一点,本文是原创的并不是从其它地方转载.所有的数据是我从 GitHub 和 RubyGems 上码下来的,数据的截取时间就是本文的发布日期. RubyGems 的下载量可以看到在用这个 g ...
- 【转】教你Ruby快速入门
转自:http://developer.51cto.com/art/200703/41243.htm 介绍 这是一个短小的Ruby入门,完全读完只需20分钟.这里假设读者已经安装了Ruby,如果你没有 ...
- 《ruby编程语言》笔记 1
赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...
- ruby klb.rb irb
1.字符串格式化 Python "%s=%s" % (k, v) 在阅读 Python 字符串格式化的时候,视线先看到字符串的 %s 字样,但是不知道这指的是什么,然后看后面的变量 ...
- Ruby:多线程队列(Queue)下载博客文章到本地
Ruby:多线程下载博客文章到本地的完整代码 #encoding:utf-8 require 'net/http' require 'thread' require 'open-uri' requir ...
随机推荐
- Web系统常见安全漏洞及解决方案-SQL盲注
关于web安全测试,目前主要有以下几种攻击方法: 1.XSS 2.SQL注入 3.跨目录访问 4.缓冲区溢出 5.cookies修改 6.Htth方法篡改(包括隐藏字段修改和参数修改) 7.CSRF ...
- C#设计模式之代理模式(三)
15.4 远程代理 远程代理(Remote Proxy)是一种常用的代理模式,它使得客户端程序可以访问在远程主机上的对象,远程主机可能具有更好的计算性能与处理速度,可以快速响应并处理客户端的请求. ...
- Oracle往列中插入html代码
开发提了一个需求,需要往模板表中插入包含html代码的记录,表的ddl如下 create table WZ_SITEMSGTEMPLATE ( id ) not null, templateconte ...
- 如何批量删除Redis数据库中的Key
借助 Linux 的 xargs 指令来完成 redis-cli keys "*" | xargs redis-cli del //如果redis-cli没有设置成系统变量,需要指 ...
- expdp/impdp使用sysdba权限迁移数据
expdp 'userid="/ as sysdba"' directory=DATA_PUMP_DIR full=y logfile=fullexp.log estimate_o ...
- Fiori Launchpad Tile点击后跳转的调试技巧
在SAP Fiori launchpad 里点击某个tile之后,后台会计算出跳转的目标url返回给前台. 下图中一个个白色的方框就成为tile.每个tile点击之后,会打开一个对应的Fiori应用. ...
- BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- [18/11/26] this关键字、static关键字和静态块(及语句块)
1.this关键字 this的本质就是“创建好的对象的地址”! 由于在构造方法调用前,对象已经创建.因此,在构造方法中也可以使用this代表“当前对象” [用法] 1. 在程序中产生二义性之处 ...
- Golang Failpoint 的设计与实现
小结: 1. https://mp.weixin.qq.com/s/veIoupLjM4l5SUVC6h_Gkw Golang Failpoint 的设计与实现 原创: 龙恒 PingCAP 今天
- 在Spring整合aspectj实现aop的两种方式
-----------------------------基于XML配置方案目标对象接口1 public interface IUserService { public void add(); pub ...