使用ruby中的Net :: HTTP代理通过代理访问网站

时间:2011-06-28 00:35:49

标签: ruby http sockets proxy proxy-classes

我正在尝试使用Net :: HTTP :: Proxy访问QA环境网站来获取响应。但是每当我尝试连接时,我都会收到SocketError。请找到我正在尝试使用的代码段。

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>'

通过配置浏览器的autoproxy设置,我可以使用Selenium访问同一个网站。但我需要通过Net :: HTTP获得它的响应。如果还有其他办法,请告诉我。

3 个答案:

答案 0 :(得分:1)

尝试从代理地址中删除http://

答案 1 :(得分: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|}

答案 2 :(得分: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"))