• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

rubyhttp爬虫中的:body用法问题

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
require 'http'
url = 'http://localhost/b.php'
data = 'whoami=whoami'
html = HTTP.via('127.0.0.1',8080).headers('Content-Type'=> 'application/x-www-form-urlencoded').post(url, :body => data)
puts html

 引用perl6 中的 User-agent模块中的一段文本:

Adds the form data, supplied either as a Hash, an Array of Pair,
or in a named parameter style, to the POST request (it doesn't
make sense on most other request types.) The default is to use
'application/x-www-form-urlencoded' and 'multipart/form-data' can be used
by providing the ':multipart' adverb.  Alternatively a previously applied
"content-type" header of either 'application/x-www-form-urlencoded'
or 'multipart/form-data' will be respected and in the latter case any
applied boundary marker will be retained.

 原文出处:

https://github.com/sergot/http-useragent/blob/master/lib/HTTP/Request.pm6

perl6中, http-useragent中的 add-form-data 函数会自动添加如下头:

Content-Type : application/x-www-form-urlencoded

但在 ruby http模块中, 当你发送如下POST数据时:

data = "cmd=whoami"

html = HTTP.post(url, body:data)

原始数据头为:

POST /b.php HTTP/1.1
Connection: close
Host: localhost
User-Agent: http.rb/3.0.0
Content-Length: 13

cmd=whoami

 

些数据并没有 Contype-Type: application/x-www-form-urlencoded 头, 此时虽然发送了数据过去, 但服务端并不会接收到(因为少了Contype-Type:app......这一个header)

如果改用如下代码, Contype-Type: appli..... 头会默认为你加上。

data = {‘cmd’=>'whoami'}

html = HTTP.post(url, form:data)

原始数据包为:

POST /b.php HTTP/1.1
Connection: close
Content-Type: application/x-www-form-urlencoded
Host: localhost
User-Agent: http.rb/3.0.0
Content-Length: 13

cmd=whoami

 

 

form与body的差别是, form为hash类型,  body为字符串型。

为了方使测试, 还是body比起form方便得多。

只是每次使用时, 记得加上:

Content-Type: application/x-www-form-urlencoded

 

示例代码:

require 'http'
url = 'http://localhost/b.php'
data = 'whoami=whoami'
html = HTTP.via('127.0.0.1',8080).headers('Content-Type'=> 'application/x-www-form-urlencoded').post(url, :body => data)
puts html

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Mac下安装homeBrew和ruby发布时间:2022-07-18
下一篇:
c,php,ruby,python按行循环读取文件内容发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap