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`

提前谢谢

4

2 回答 2

0

If you proxy your mechanize and wget requests through a debugging proxy such as fiddler or charles you can compare them side by side and thereby better understand the problem.

于 2012-04-24T12:03:54.243 回答
0

你试过Mechanize::Download吗?

这是一个参考

使用 WWW:Mechanize 将文件下载到磁盘,而无需先将其全部加载到内存中

于 2015-01-10T11:42:01.657 回答