2011-06-03 81 views
0

我试图使用Mechanize捕获POST请求,这是不可能通过窗体,因为窗体是在防止通过JavaScript直接加载的iframe中。使用机械化POST请求捕获

HTTP头中,谷歌Chrome浏览器为例请求如下(注意paradalinea参数)

Request URL:http://www.etr.gov.ar/getSmsResponse.php 
Request Method:POST 
Status Code:200 OK 
Request Headers 
Accept:*/* 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:es-419,es;q=0.8 
Connection:keep-alive 
Content-Length:21 
Content-Type:application/x-www-form-urlencoded 
Host:www.etr.gov.ar 
Origin:http://www.etr.gov.ar 
Referer:http://www.etr.gov.ar/cont-cuandollega.php 
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.30 Safari/534.30 
X-Requested-With:XMLHttpRequest 

Form Dataview URL 
parada:4152 
linea:112 

Response Headers 
Connection:close 
Content-Length:111 
Content-Type:text/html 
Date:Fri, 03 Jun 2011 02:35:45 GMT 
Server:Microsoft-IIS/7.5 
X-Powered-By:PHP/5.1.2 
ASP.NETl 

而对于这个例子中,内容是:

Linea 112N: 0min. 379mts., siguiente 25min. 9937mts. - Linea 112R: 81min. 24349mts., siguiente 101min. 30548mts 

我”什么已尝试机械化是以下ruby脚本,但我得到一个空白页作为回应:

require 'mechanize' 
agent = WWW::Mechanize.new 
agent.post("http://www.etr.gov.ar/getSmsResponse.php", "parada" => "4152", "linea"=>"112") 

我会做错什么?非常感谢你。

更新:将POST作为散列传递完美。为了呈现内容,我只需要做agent.post.content

回答

-2

post方法需要这些参数作为散列。尝试:

agent.post( “http://www.etr.gov.ar/getSmsResponse.php”,{ “帕拉达”=> “4152”, “LINEA”=> “112”})

+1

我投下来,因为有一个在路上没有区别的Ruby这个代码之间的解释参数和原始代码。这不是解决问题。 – Kai 2011-06-10 02:03:39

+0

够公平的。感谢您指出这一点。我测试了它,你是对的。 – mightilybix 2011-06-27 13:33:55

1

其实,你原来的代码工作正常。您只需要将其打印为agent.post.content即可查看结果。


要到mightilybix的答案回应:

你的代码工作,而无需使用经过哈希的原因{和}是因为Ruby也有在那里,如果你传递一个哈希作为一个函数的最后一个参数,然后一个功能你不需要包括大括号。例如:

def test(str, params) 
    puts str 
    params.each { |param| puts param } 
end 

呼叫:

test("hello", {"animal" => "cat", "gender" => "m"}) 

是完全一样的东西叫:

test("hello", "animal" => "cat", "gender" => "m") 
+0

谢谢你的解释。我没有意识到这一点 – bruno077 2011-06-10 12:57:49