accessing a website through a proxy using Net::HTT

2019-08-09 05:46发布

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.

3条回答
smile是对你的礼貌
2楼-- · 2019-08-09 05:57
#!/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楼-- · 2019-08-09 05:57

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"))
查看更多
我只想做你的唯一
4楼-- · 2019-08-09 05:58

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

查看更多
登录 后发表回答