accessing a website through a proxy using Net::HTTP proxy in ruby

╄→гoц情女王★ 提交于 2019-12-11 06:19:09

问题


I'm trying to access a QA environment website using Net::HTTP::Proxy to get the response.But I keep getting a SocketError whenever I try to connect. Please find the code snippet that I'm trying to use.

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

This is the Error I'm getting

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

I'm able to access the same website using Selenium by configuring the autoproxy settings of the browser. But I need to get the response of it through Net::HTTP. Please let me know if there is any other way of doing it.


回答1:


try removing the http:// from your proxy address.




回答2:


#!/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|}



回答3:


Net::HTTP accepts URI's for the start and get classes, so you could change your code to something like this:

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"))


来源:https://stackoverflow.com/questions/6500461/accessing-a-website-through-a-proxy-using-nethttp-proxy-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!