Ruby open-uri代理身份验证失败 [英] Ruby open-uri proxy authentication fails

查看:240
本文介绍了Ruby open-uri代理身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nokogiri编写本机Ruby脚本来废弃网站,每当我将代理选项传递给open-uri open()方法时,它返回 407需要代理身份验证但我的选项确实有身份验证详细信息,这是我的代码

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

推荐答案

我在工作中有限制性代理但是以下工作。
尝试使用您的代码代理凭证。
我在这里使用Nokogiri进行解析,但你并不需要它来获取HTML。

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

这篇关于Ruby open-uri代理身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆