2011-06-28 83 views
1

我試圖訪問一個使用Net :: HTTP :: Proxy的QA環境網站來獲取響應。但是當我嘗試訪問時,我總是收到SocketError連接。請找到我正在嘗試使用的代碼段。通過代理使用net :: HTTP代理在ruby中訪問網站

proxy_addr = "http://autoproxy1.qa.com" 
proxy_class = Net::HTTP::Proxy(proxy_addr,80).start("mywebsite.com") 

這是我得到

SocketError: getaddrinfo: Name or service not known 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:581:in `initialize' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:581:in `open' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:581:in `block in connect' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/timeout.rb:44:in `timeout' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/timeout.rb:82:in `timeout' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:581:in `connect' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:574:in `do_start' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:569:in `start' 
    from /site/ruby/ruby-1.9.1-4/lib/ruby/1.9.1/net/http.rb:453:in `start' 
    from (irb):6 
    from /site/ruby/ruby-1.9.1-4/bin/irb:12:in `<main>' 

錯誤我能夠通過配置瀏覽器的自動代理設置訪問使用硒同一個網站。但我需要通過Net :: HTTP獲得它的響應。請讓我知道是否有其他方式。

回答

0

Net::HTTP接受URI的起始和獲取類,所以你可以更改您的代碼是這樣的:

require 'uri' 
require 'net/http' 
proxy_addr = "http://autoproxy1.qa.com" 
proxy_port = 80 
proxy_class = Net::HTTP::Proxy(proxy_addr, proxy_port).start(URI("mywebsite.com")) 
1

嘗試從您的代理地址刪除的http://

1
#!/usr/bin/ruby1.9.3 
require 'net/http' 

proxy_addr = '109.104.128.130' 
proxy_port = 8741 

Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.templesec.org') {|http|}