使用OpenURI

时间:2015-04-22 13:56:58

标签: ruby https proxy nokogiri open-uri

我在代理后面,我必须获得一个HTTPS网页来收集一些信息,但OpenURI会返回一个错误:“非HTTP代理URI”。

这就是问题所在:

> yadayada@ubuntu:~/Desktop/test/lib$ ruby JenkinsTest.rb 
/home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:257:in `open_http': Non-HTTP proxy URI: https://web-proxy.yadayada:8088 (RuntimeError)
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:736:in `buffer_open'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:211:in `block in open_loop'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `catch'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `open_loop'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:716:in `open'
from /home/yadayada/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:34:in `open'
from JenkinsTest.rb:6:in `<main>'
yadayada@ubuntu:~/Desktop/test/lib$ 

这是我正在运行的代码:

  1 require 'rubygems'
  2 require 'nokogiri'
  3 require 'open-uri'
  4 
  5 # Request the Jenkins webpage
  6 @jenkinsWebPage = Nokogiri::HTML(open("https://yadayad.yada.yada.com:8443"))
  7 
  8 # Prints the received page
  9 puts @jenkinsWebPage 

代理没有登录名/密码。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我已经找到了如何获取页面,但我必须切换open-uri for net / https,同样,我将OpenSSL设置为VERIFY_NONE,因为它是自签名证书(公司服务器):< / p>

require 'rubygems'
require 'nokogiri'
require 'net/https'
require 'openssl'

class JenkinsTest
    # Request the Jenkins webpage
    def request_jenkins_webpage
        uri = URI.parse("https://https://yadayad.yada.yada.com:8443")
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        request = Net::HTTP::Get.new(uri.request_uri)
        response = http.request(request)
        @@page = Nokogiri::HTML(response.body)
    end

    def print_jenkins_webpage
        puts @@page
    end
end

看起来很难看,如果有人发现更好的方法,请编辑这篇文章,但截至目前,它的工作正常。