Ruby open-uri proxy authentication fails

可紊 提交于 2019-12-11 11:33:53

问题


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


来源:https://stackoverflow.com/questions/32818853/ruby-open-uri-proxy-authentication-fails

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