如何在rubys net/http中设置代理?

5lwkijsr  于 2023-02-03  发布在  Ruby
关注(0)|答案(5)|浏览(167)

我尝试设置一个代理,并在一个简单的get请求中使用它,就像在documentation中一样。但是我总是收到一个错误!地址和端口与open-uri是正确的。

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
  # always proxy via your.proxy.addr:8080
  Net::HTTP.get('google.de', '')
}

我做错了什么?谢谢你的回答!

chhkpiq4

chhkpiq41#

还有另一种选择:
Net::HTTP将自动从http_proxy环境变量创建一个代理(如果存在)。
所以你可以用

ENV['http_proxy'] = 'http://172.16.3.160:4226' # your http://address:port here

并且Net::HTTP默认情况下会将其用于所有请求。
它对于第三方库中的net_http请求很有帮助(例如,它适用于gem gibbon for MailChimp)。
为代理地址传递nil以禁用默认http_proxy

qeeaahzv

qeeaahzv2#

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|

这将创建一个http对象供您在块中使用。使用该对象而不是每次都生成新对象,这里是Net::HTTP.get('google.de', '')

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
  # always proxy via your.proxy.addr:8080
  http.get('google.de', '')
}
brjng4g3

brjng4g33#

下面是在代理后进行REST API调用的代码:

require "uri"
require 'net/http'
    
proxy_host = '<proxy addr>'
proxy_port = '<proxy_port'
proxy_user = '<username>'
proxy_pass = '<password>'
    
uri   = URI.parse("https://saucelabs.com:80/rest/v1/users/<username>")
proxy = Net::HTTP::Proxy(proxy_host,
                         proxy_port,
                         proxy_user,
                         proxy_pass)
    
req = Net::HTTP::Get.new(uri.path)

req.basic_auth(<sauce_username>,
               <sauce_password>)
    
result = proxy.start(uri.host,uri.port) do |http|
  http.request(req)
end
    
puts result.body
ajsxfq5m

ajsxfq5m4#

我试了以上所有的方法,但都不起作用。
我最后的脚本如下(为我工作):

url = "https://host.com/url"
proxy_url = "http://proxy.com:port"

# split important values
uri = URI(url)
proxy_array = proxy_url.split(":")
proxy_protocol = proxy_array[0]
proxy_addr = proxy_array[1]
proxy_addr = proxy_addr[2 .. proxy_addr.length - 1]
proxy_port = proxy_array[2].to_i

# new objects
proxy = Net::HTTP::Proxy(proxy_addr, proxy_port)
result = proxy.get(uri);
093gszye

093gszye5#

#!/usr/bin/env ruby
#

require 'rubygems' 
require 'net/http' 
require 'open-uri' 
require 'timeout' 

mimvp_url = "http://proxy.mimvp.com/exist.php"
mimvp_url2 = "https://proxy.mimvp.com/exist.php"
mimvp_url3 = "https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js"

$proxy = '183.222.102.95:8080'

$proxy_addr = $proxy.split(':')[0].strip
$proxy_port = $proxy.split(':')[1].strip

puts $proxy_addr
puts $proxy_port

begin 
  Timeout.timeout(30) {

    # mimvp_url = http://proxy.mimvp.com/exist.php
    # uri.host = proxy.mimvp.com
    # uri.port = 80
    # uri.path = /exist.php
    uri = URI.parse(mimvp_url)
    result = Net::HTTP.new(uri.host, nil, $proxy_addr, $proxy_port).start { |http|
      http.get(uri.path)
    }
    puts result.body

    # mimvp_url = http://proxy.mimvp.com/exist.php
    # uri.host = proxy.mimvp.com
    # uri.port = 80
    # uri.path = /exist.php
    # req = #<Net::HTTP::Get:0x007fafa594ff78>
    uri = URI.parse(mimvp_url)
    req = Net::HTTP::Get.new(uri.path)
    result = Net::HTTP::Proxy($proxy_addr, $proxy_port).start(uri.host, uri.port) {|http| 
      http.request(req)
    } 
    puts result.body

    # proxy auth (NO TEST)
    $proxy_addr = '<proxy addr>'
    $proxy_port = '<proxy_port'
    $proxy_user = '<username>'                # 代理用户名
    $proxy_pass = '<password>'                # 代理密码

    $website_username = '<website_username>'  # 目标网站登录用户名
    $website_password = '<website_password>'  # 目标网站登录密码

    uri = URI.parse(mimvp_url)
    req = Net::HTTP::Get.new(uri.path)
    req.basic_auth($website_username, $website_password)

    result = Net::HTTP::Proxy($proxy_addr, $proxy_port, $proxy_user, $proxy_pass).start(uri.host, uri.port) {|http| 
      http.request(req)
    } 
    puts result.body

}
rescue => e  
    puts e.inspect  
    exit  
end

相关问题