Ruby open-uri proxy authentication fails

2019-08-05 17:04发布

问题:

I'm coding a native Ruby script to scrap a website using Nokogiri, whenever I pass proxy options to the open-uri open() method, it returns 407 Proxy Authentication Required but my options does have the authentification details, here's my code

proxy_url = URI.parse("http://12.34.567.89:PORT")
session = Nokogiri::HTML(open("http://google.com", :proxy_http_basic_authentication =>[proxy_url, "username", "password"]

Note: As my proxy is premium, I have replaced real proxy credentials with fake one

回答1:

I have a restrictive proxy at work but the followig works. Try the code with your proxy credentials. I used Nokogiri here for parsing but you don't realy need it for getting the HTML.

require 'net/http' 
require 'uri'
require 'nokogiri'

url = 'http://stackoverflow.com/questions/32818853/ruby-open-uri-proxy-authentication-fails'

proxy_host, proxy_port, proxy_user, proxy_pass = '****', 8080, "*****", "*****" 
uri = URI.parse(url)

Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).start(uri.host, uri.port) do |http|
  http.get(uri.path) do |str|
    puts Nokogiri::HTML(str).text
  end
end