zoukankan      html  css  js  c++  java
  • ruby http爬虫中的 :body 用法问题

    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
  • 相关阅读:
    寒假学习(九)
    寒假学习(八)利用Sqoop导出Hive分析数据到MySQL库
    寒假学习(七)热词统计
    寒假学习(六)统计学生成绩
    寒假学习(五)模拟图形绘制
    寒假学习(四)编写MapReduce程序清洗信件内容数据
    寒假学习(三)北京市政百姓信件分析
    实现地图输出的相关知识
    使用Node.js+Socket.IO搭建WebSocket实时应用
    centos6 安装python3.5后pip无法使用的处理
  • 原文地址:https://www.cnblogs.com/perl6/p/8045207.html
Copyright © 2011-2022 走看看