如何以编程方式重复表单 POST 请求

Posted

技术标签:

【中文标题】如何以编程方式重复表单 POST 请求【英文标题】:How do I repeat the form POST request programmatically 【发布时间】:2015-05-30 11:51:16 【问题描述】:

当我访问该网站时http://www.jetstar.com/au/en/home

填写表格然后提交。

它将发送一个 POST 请求,然后将我重定向到显示票价页面(html)的新页面。

我可以在第二个 GET 响应中得到预期的结果

但是,当我尝试使用 RubyCharles

重复 POST 请求时

我会得到 302 Found 错误。

我不明白。

红宝石

q_prams =   
   "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListFareTypes"   =>"I",
   "ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDay1"   =>"9",
   ~~~
   "pageToken"   =>"sLkmnwXwAsY=",
   "ControlGroupSearchView$AvailabilitySearchInputSearchView$fromCS"   =>"yes"


res = RestClient.post 'https://booknow.jetstar.com/Search.aspx', q_prams

POST 请求参数

ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListCurrency=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListFareTypes=I&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketDay1=18&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketDay2=1&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketDay3=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketMonth1=2015-6&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketMonth2=1968-1&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListMarketMonth3=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListPassengerType_ADT=1&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListPassengerType_CHD=0&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24DropDownListPassengerType_INFANT=0&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24RadioButtonMarketStructure=OneWay&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketDestination1=MEL&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketDestination2=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketDestination3=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketOrigin1=NAN&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketOrigin2=&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24TextBoxMarketOrigin3=&ControlGroupSearchView%24ButtonSubmit=&__VIEWSTATE=&culture=en-AU&date_picker=&go-booking=&pageToken=sLkmnwXwAsY%3D&ControlGroupSearchView%24AvailabilitySearchInputSearchView%24fromCS=yes&_pe_39b5379c652b_9df496572198=null&locale=en-AU

第一次响应(SUCCESS)但猫不会以编程方式重复它

<!doctype html><!--paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/--><!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]--><!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]--><!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]--><!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]--><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head class="SB">
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Jetstar Airways Cheap Flights, Low Fares all day everyday from the world's best Cheap Fare airline</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  <link rel="icon" href="favicon.ico" type="image/ico">
  <link rel="SHORTCUT ICON" href="favicon.ico">
...

【问题讨论】:

302 不是错误代码,它是 HTTP 重定向。服务器将您重定向到哪里?这可能会提供线索。 【参考方案1】:

这很可能是因为CSRF verification,站点用户 CSRF 验证请求以确保表单提交来自同一站点。

在您的情况下,您尝试从其他来源提交表单,因此验证失败。

如果您想执行上述操作,我会使用 capybara 之类的库进行屏幕抓取

阅读更多关于 CSRF here

【讨论】:

以上是关于如何以编程方式重复表单 POST 请求的主要内容,如果未能解决你的问题,请参考以下文章

java程序如何获取POST方式提交的数据?

如何在不使用 HTML 表单的情况下以编程方式将 POST 请求发送到 JSF 页面?

用request作post请求,需要有key值重复的form表单怎么做

如何创建包含多部分表单数据的可重复 POST 请求?

无法以编程方式提交表单[重复]

Wordpress:如何以编程方式创建帖子并在之前检查重复项