使用OpenURI或net/http进行Ruby代理身份验证GET/POST [英] Ruby Proxy Authentication GET/POST with OpenURI or net/http

查看:108
本文介绍了使用OpenURI或net/http进行Ruby代理身份验证GET/POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ruby 1.9.3,并尝试使用open-uri获取URL并尝试使用Net:HTTP

I'm using ruby 1.9.3 and trying to use open-uri to get a url and try posting using Net:HTTP

我试图同时使用代理身份验证:

Im trying to use proxy authentication for both:

尝试使用net/http发出POST请求:

require 'net/http'
require 'open-uri'
http = Net::HTTP.new("google.com", 80)
headers = { 'User-Agent' => 'Ruby 193'}

resp, data = http.post("/", "name1=value1&name2=value2", headers)
puts data

对于open-uri我无法使用的POST我使用:

And for open-uri which I can't get to do POST I use:

data = open("http://google.com/","User-Agent"=> "Ruby 193").read

我将如何修改它们以使用具有HTTP身份验证的代理

How would I modify these to use a proxy with HTTP Authentication

我已经尝试过(对于开放式uri)

I've tried (for open-uri)

data = open("http://google.com/","User-Agent"=> "Ruby 193", :proxy_http_basic_authentication => ["http://proxy.com:8000/", "proxy-user", "proxy-password"]).read

但是我只能得到一个OpenURI::HTTPError: 407 Proxy Authentication Required.我已经验证了所有内容,并且可以在浏览器中使用相同的身份验证和代理详细信息,但是我无法获得ruby的帮助.

However all I will get is a OpenURI::HTTPError: 407 Proxy Authentication Required. I've verified all and it works in the browser with the same authentication and proxy details but I can't get ruby to do it.

如何修改上面的代码以正确添加http身份验证?有没有人经历过这种暴行?

How would I modify the code above to add http authentication properly? Has anyone gone through this atrocity?

推荐答案

尝试:

require "open-uri"
proxy_uri = URI.parse("http://proxy.com:8000")

data = open("http://www.whatismyipaddress.com/", :proxy_http_basic_authentication => [proxy_uri, "username", "password"]).read
puts data

对于Net :: HTTP,我最近将名为httpa的Net :: HTTP包装器实现了对具有http身份验证的代理的支持,该包装器称为 http .如果您查看我的上一个请求,您将看到基本的实现.

As for Net::HTTP, I recently implemented support for proxies with http authentication into a Net::HTTP wrapper library called http. If you look at my last pull-request, you'll see the basic implementation.

编辑:希望这可以使您朝正确的方向前进.

Hopefully this will get you moving in the right direction.

Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port,"username","password").start('whatismyipaddress.com') do |http| 
  puts http.get('/').body
end

这篇关于使用OpenURI或net/http进行Ruby代理身份验证GET/POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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