gpt4 book ai didi

ruby - 使用 ruby​​ mechanize 保存文件

转载 作者:行者123 更新时间:2023-12-04 16:22:28 24 4
gpt4 key购买 nike

我有代码问题(此代码可能会获取请求和下载生成的文件):

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`

提前谢谢

最佳答案

如果您通过调试代理(例如 fiddler 或 charles)代理您的 Mechanize 和 wget 请求,您可以将它们并排比较,从而更好地了解问题。

关于ruby - 使用 ruby​​ mechanize 保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com