我正在尝试使用mechanize gem构建一个GET请求,并且我不断收到重定向错误。首先,我想知道是否有一种方法可以看到在使用不同的参数构建后发送的实际请求?
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
cookie = Mechanize::Cookie.new('xyz_session', @tokenid)
cookie.domain = ".mydomain.com"
cookie.path = "/"
@agent.cookie_jar << cookie
@agent.redirection_limit=0
puts @agent.cookies
body = {}.to_json
#@agent.set_proxy("localhost",3000)
@agent.request_headers = {'Content-Type' => "application/json"}
@agent.get("https://access.test.api.mydomain.com/oidc/v1/user/authorise?response_type=code&redirect_uri=http://localhost&client_id=testclient1&service=AccountSignInService&state=any-state")
expect(response.code).to eql(302), "Authorization Code couldn't be received"
我不断获得Redirect limit of 0 reached (Mechanize::RedirectLimitReachedError)
如果我没有设置重定向限制,我会得到connection refused: localhost:3000 (Net::HTTP::Persistent::Error)
因此,我想首先检查我的请求是否按照我希望的方式发送...
答案 0 :(得分:2)
只需为代理设置记录器即可获得请求/响应调试。
require 'mechanize'
require 'logger'
agent = Mechanize.new
agent.log = Logger.new(STDERR)
agent.get('http://google.com/')
运行此代码将产生:
$ bundle exec ruby ./mech.rb I, [2016-06-29T13:14:07.019088 #26165] INFO -- : Net::HTTP::Get: / D, [2016-06-29T13:14:07.019211 #26165] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity D, [2016-06-29T13:14:07.019247 #26165] DEBUG -- : request-header: accept => */* D, [2016-06-29T13:14:07.019281 #26165] DEBUG -- : request-header: user-agent => Mechanize/2.7.4 Ruby/2.1.5p273 (http://github.com/sparklemotion/mechanize/) D, [2016-06-29T13:14:07.019324 #26165] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7 D, [2016-06-29T13:14:07.019365 #26165] DEBUG -- : request-header: accept-language => en-us,en;q=0.5 D, [2016-06-29T13:14:07.019399 #26165] DEBUG -- : request-header: host => google.com I, [2016-06-29T13:14:07.056163 #26165] INFO -- : status: Net::HTTPMovedPermanently 1.1 301 Moved Permanently D, [2016-06-29T13:14:07.056255 #26165] DEBUG -- : response-header: location => http://www.google.com/ D, [2016-06-29T13:14:07.056289 #26165] DEBUG -- : response-header: content-type => text/html; charset=UTF-8 D, [2016-06-29T13:14:07.056320 #26165] DEBUG -- : response-header: date => Wed, 29 Jun 2016 11:14:07 GMT D, [2016-06-29T13:14:07.056352 #26165] DEBUG -- : response-header: expires => Fri, 29 Jul 2016 11:14:07 GMT D, [2016-06-29T13:14:07.056386 #26165] DEBUG -- : response-header: cache-control => public, max-age=2592000 D, [2016-06-29T13:14:07.056456 #26165] DEBUG -- : response-header: server => gws D, [2016-06-29T13:14:07.056505 #26165] DEBUG -- : response-header: content-length => 219 D, [2016-06-29T13:14:07.056536 #26165] DEBUG -- : response-header: x-xss-protection => 1; mode=block D, [2016-06-29T13:14:07.056568 #26165] DEBUG -- : response-header: x-frame-options => SAMEORIGIN D, [2016-06-29T13:14:07.056706 #26165] DEBUG -- : Read 219 bytes (219 total) I, [2016-06-29T13:14:07.057585 #26165] INFO -- : follow redirect to: http://www.google.com/ I, [2016-06-29T13:14:07.058406 #26165] INFO -- : Net::HTTP::Get: / D, [2016-06-29T13:14:07.058454 #26165] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity D, [2016-06-29T13:14:07.058483 #26165] DEBUG -- : request-header: accept => */* D, [2016-06-29T13:14:07.058511 #26165] DEBUG -- : request-header: user-agent => Mechanize/2.7.4 Ruby/2.1.5p273 (http://github.com/sparklemotion/mechanize/) D, [2016-06-29T13:14:07.058540 #26165] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7 ...
答案 1 :(得分:0)
如果我没有设置重定向限制,我会拒绝连接: localhost:3000(Net :: HTTP :: Persistent :: Error)
如果没有服务器侦听ruby程序中指定的端口,我可以复制该错误。要解决该错误,您需要启动一个侦听端口3000的服务器,例如netcat
这是一个简单的netcat命令:
$ nc -l 3000
告诉netcat(nc)在端口3000上监听(-l)。然后如果我在另一个终端窗口中运行以下ruby程序:
require 'mechanize'
agent = Mechanize.new
cookie = Mechanize::Cookie.new("hello", "world")
cookie.domain = "localhost"
cookie.path = "/"
agent.cookie_jar.add(cookie)
agent.request_headers = {'Content-Type' => "application/json"}
page = agent.get("http://localhost:3000/get_page")
netcat窗口显示:
GET /get_page HTTP/1.1
Accept-Encoding: gzip,deflate,identity
Accept: */*
User-Agent: Mechanize/2.7.4 Ruby/2.3.0p0 (http://github.com/sparklemotion/mechanize/)
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Cookie: hello=world
Host: localhost:3000
Content-Type: application/json
Connection: keep-alive
Keep-Alive: 300
在谷歌上做各种各样的事情有很多netcat配方。