• 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
  • 相关阅读:
    spring声明式事务管理详情解析
    Nginx nginx.conf配置文件详细说明
    etcd
    rsyslog使用简介
    LINUX常见命令
    kafka简介&使用
    kafka安装
    Zookeeper简介&应用场景
    Zookeeper安装
    安装JDK
  • 原文地址:https://www.cnblogs.com/perl6/p/8045207.html
Copyright © 2020-2023  润新知