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 ...
随机推荐
- Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)
有时在Sharepoin中有些执行任务可能会超过Sharepoint环境默认的Timout限制,这种情况下系统会报"Request Timed out"错误.对此我们可以在两个层次 ...
- Selenium2学习(十)-- iframe定位
前言 有很多小伙伴在拿163作为登录案例的时候,发现不管怎么定位都无法定位到,到底是什么鬼呢,本篇详细介绍iframe相关的切换 以http://mail.163.com/登录页面10为案例,详细介绍 ...
- 随tomcat启动的Servlet程序
由于需要做一定定时轮询程序,自己写了一个Servlet小程序,在Servlet里面的Init函数中做一个Timer,定时执行程序. 代码如下: public class MailStartup ex ...
- 【转】vector中对象指针的排序
原文:http://blog.csdn.net/tanlijun37/article/details/1948493 vector中对象指针的排序,初步想法是1: 把对象指针存到vector,重载bo ...
- [EffectiveC++]item30:Understand the ins and outs of inlining
- 解决Myeclipse报PermGen space异常的问题
最近使用eclipse做开发,使用的服务器是tomcat,但在启动时报了Caused by: java.lang.OutOfMemoryError: PermGen space的异常. 这个错误很常见 ...
- SQL SERVER 2012断日志
有一个SQL2012库的日志达到了100G左右,平时开发人员根本没有做过事务日志备份,而磁盘空间已经快满了.所以,只能截断它.但是,由于从2K8以后,SQL SERVER好像不再提供 truncate ...
- 初识prufer序列
前言 \(prufer\)序列应该是一个比较实用的东西. 据\(hl666\)大佬说,一切与度数有关的树上计数问题,都可以用它以及它的性质来解决. 而听说\(ZJOI\)最近特别喜欢出计数题,所以有必 ...
- HDU 4165 卡特兰
题意:有n个药片,每次吃半片,吃2n天,那么有多少种吃法. 分析:如果说吃半片,那么一定要吃过一整片,用 ) 表示吃半片,用 ( 表示吃整片,那么就是求一个正确的括号匹配方案数,即卡特兰数. 卡特兰数 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...