如何使用watir捕获HTTP完整标头

我需要在请求发出后捕获HTTP响应。 我尝试过使用“net / http”gem,但它没有给我完整的响应标题。 我试过的代码是

uri = URI("http:/example.com") res = Net::HTTP.get_response(uri) res.to_hash 

我收到一些响应标题但不是完整的标题,我在firebug中检查了相同的请求,并且它提供了一些额外的标题,我通过我的代码获得了

任何人都可以帮助我获取完整的HTTP响应标头,或通过调用浏览器来做到这一点。

也许这会有所帮助: WebDriver:YU没有HTTP状态代码?!

试试这个:

 uri = URI("http:/example.com") res = Net::HTTP.get_response(uri) res.header.to_hash 

如果你想获取标题信息,watir可能是你错误的库。 你想解决什么问题?

不是一个完整的答案,但更多的方向:)

不可能使用Webdriver(请参阅http://jimevansmusic.blogspot.nl/2012/07/webdriver-yu-no-have-http-status-codes.html )。

可能的解决方案:

  1. 使用Selenium :: Client
  2. 使用代理

解决方案1(Selenium :: Client):

您可以使用Selenium(也由Watir Webdriver使用)来完成。 点击此处: http : //blog.testingbot.com/2011/12/21/capture-network-traffic-with-selenium

 require "rubygems" gem "selenium-client" require "selenium/client" gem 'test-unit' require 'test/unit' # since this code comes from their site (should not be needed) gem "testingbot" require "testingbot" class ExampleTest < TestingBot::TestCase attr_reader :browser def setup @browser = Selenium::Client::Driver.new \ :host => "hub.testingbot.com", :port => 4444, :browser => "firefox", :version => "8", :platform => "WINDOWS", :url => "http://www.google.com", :timeout_in_second => 60 browser.start_new_browser_session(:captureNetworkTraffic => true) end def teardown browser.close_current_browser_session end def test_command browser.open "/" p browser.browser_network_traffic end end 

根据文章,这将在Firefox 8中打开谷歌并返回网络流量。 响应的一个例子是:

 "403 GET http://localhost:5555/favicon.ico1333 bytes 94ms (2011-12-21T15:53:06.352+0100 - 2011-12-21T15:53:06.446+0100 Request Headers - Host => localhost:5555 - User-Agent => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 - Accept => image/png,image/*;q=0.8,*/*;q=0.5 - Accept-Language => en-us,en;q=0.5 - Accept-Encoding => gzip, deflate - Accept-Charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7 - Proxy-Connection => keep-aliveResponse Headers - Date => Wed, 21 Dec 2011 14:53:06 GMT - Server => Jetty/5.1.x (Windows 7/6.1 x86 java/1.6.0_26 - Content-Type => text/html - Content-Length => 1333 - Via => 1.1 (jetty) 

解决方案2(代理):

与https://github.com/jarib/browsermob-proxy-rb一起检查http://bmp.lightbody.net/ 。