繁体   English   中英

如何在Ruby中模拟http发布请求并解析html响应

[英]How to simulate http post request and parse html response in ruby

我正在尝试模拟与此网页上的表单相关的http请求: http : //www.ei.applipub-fft.fr/eipublic/competitionRecherche.do然后,我需要解析响应html。

我试图使用宝石“ webmock”和“ rest-client”。 因此,在研究了原始的HTTP请求之后,我尝试了类似的操作:

def post
  stub_request(:post, "http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do").
      with(:body => {"data"=>{"dispatch"=>"filtrer", "hoi_atp"=>"T", "dtdate"=>"30/12/2015", "mois"=>"12", "multi_lig_cod"=>"0", "lig_cno_1"=>"01", "bidf"=>"0", "imit_categ_age"=>"0", "hoiCtmcDames"=>"T"}},
           :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'174', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
      to_return(:status => 200, :body => "", :headers => {})

 resp = RestClient.post('http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do', "data[dispatch]=filtrer&data[hoi_atp]=T&data[dtdate]=30/12/2015&data[mois]=12&data[multi_lig_cod]=0&data[lig_cno_1]=01&data[bidf]=0&data[imit_categ_age]=0&data[hoiCtmcDames]=T",
  :content_type => 'application/x-www-form-urlencoded')
 puts resp a
end

但是我无法获得正确的html响应。 有人可以解释所有这些工作原理吗?

我通常会使用HTTParty gem。 它允许您用Ruby编写发布请求。 例:

HTTParty.post('http://example.com/post_here', 
  body: { subject: 'This is the screen name', 
          description: 'This is the description for the problem'
        }.to_json,
  headers: { 'Content-Type' => 'application/json' } )

参考

帖子:

require "net/http"
require "uri"

uri = URI.parse("http://example.com/search")

# Shortcut
response = Net::HTTP.post_form(uri, {"q" => "My query", "per_page" => "50"})

# Full control
http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"q" => "My query", "per_page" => "50"})

response = http.request(request)

处理响应对象:

response.code             # => 301
response.body             # => The body (HTML, XML, blob, whatever)
# Headers are lowercased
response["cache-control"] # => public, max-age=2592000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM