2012-04-24 196 views
0

我有一個代碼問題(此代碼可能獲得請求&下載生成的文件):保存紅寶石文件機械化

require 'rubygems' 
require 'mechanize' 
require 'hpricot' 

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 

user = "xxx" 
pass = "xxx" 
auth_key = "xxx" 
ip = "xxx" 

agent = Mechanize.new 
page = agent.get("https://#{ip}/cgi-bin/welcome.cgi") 
form = page.forms.first 
form.username = user 
form.password = Digest::MD5.hexdigest(pass + auth_key) 
page = agent.submit form 

page = agent.get("https://#{ip}/cgi-bin/config.exp") 
form = page.forms.first 
agent.pluggable_parser.default = Mechanize::FileSaver 
agent.post("https://#{ip}/cgi-bin/config.exp", {"submitstatus" => "1"}) 

有了這個,我有一個錯誤:

/var/lib/gems/1.8/gems/mechanize-2.4/lib/mechanize/http/agent.rb:291:in `fetch': 400 => Net::HTTPBadRequest for https://31.223.225.133/cgi-bin/config.exp -- unhandled response (Mechanize::ResponseCodeError) 
from /var/lib/gems/1.8/gems/mechanize-2.4/lib/mechanize.rb:407:in `get' 
from /home/lord/Dropbox/work/ruby/ruby_backup/backup.ru:22 

如何模擬此wget請求下載文件:

`wget --no-check-certificate --load-cookies cookie --post-data='submitstatus=1' \ 
--header='Host: 10.1.25.254' \ 
--header='User-Agent: Mozilla/5.0' \ 
--header='Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \ 
--header='Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3' \ 
--header='Accept-Encoding: gzip, deflate' \ 
--header='Connection: keep-alive' \ 
--header='Referer: https://10.1.25.254/sys_setting.htm' \ 
--header='Content-Type: application/x-www-form-urlencoded' \ 
--header='Content-Length: 14' \ 
https://$ip/cgi-bin/config.exp` 

高級謝謝

+0

感謝,問題就解決了: .POST(URL,POSTDATA,{ '定製報頭'=> '自定義'}) – 2012-04-25 11:01:55

回答

0

如果您通過調試代理(如fiddler或charles)來代理機械化和wget請求,則可以將它們並排比較,從而更好地理解問題。