【问题标题】:How do I set use_ssl param when running a web request through a proxy?通过代理运行 Web 请求时如何设置 use_ssl 参数?
【发布时间】:2017-03-26 22:11:58
【问题描述】:

我使用的是 Rails 4.2.7。通过代理发送网络请求时如何设置“use_ssl”参数?我有这个

    res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port).start(uri.host, uri.port) do |http|
      http.use_ssl = (uri.scheme == "https")
      resp = http.get(uri, initheader = headers)
      status = resp.code.to_i
      if status == 302 || status == 301
        redirect = resp['location']
      end
      content = resp.body
      content_type = resp['content-type']
      content_encoding = resp['content-encoding']
    end

但是当我到达这一行时 - “http.use_ssl = (uri.scheme == "https”)” 我得到了异常

IOError: use_ssl value changed, but session already started
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:758:in `use_ssl='
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:96:in `block in get_content'
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:853:in `start'
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:584:in `start'
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:94:in `get_content'
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:33:in `get_url'
    from (irb):1
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ssl proxy net-http


    【解决方案1】:

    您是否尝试过在开始之前使用 use_ssl,如下所示

    res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port)
    res1.use_ssl = (uri.scheme == "https")
    res1.start(uri.host, uri.port) do |http|
      resp = http.get(uri, initheader = headers)
      status = resp.code.to_i
      if status == 302 || status == 301
        redirect = resp['location']
      end
      content = resp.body
      content_type = resp['content-type']
      content_encoding = resp['content-encoding']
    end
    

    【讨论】:

    • 这不起作用。这一行 -- "res1.use_ssl = (uri.scheme == "https")" 抛出错误 "Error during processing: undefined method `use_ssl=' for #<0x007fbf007b6f08>
    • <0x007fbf007b6f08>
    【解决方案2】:

    根据这个测试样本,第 109 行: https://github.com/astro/socksify-ruby/blob/master/test/tc_socksify.rb#L109

    我会尝试这样的:

    res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port).start(
      uri.host, uri.port, :use_ssl => (uri.scheme == "https")) do |http|
    
      resp = http.get(uri, initheader = headers)
      status = resp.code.to_i
      if status == 302 || status == 301
        redirect = resp['location']
      end
      content = resp.body
      content_type = resp['content-type']
      content_encoding = resp['content-encoding']
    end
    

    如果您没有有效的 SSL 证书,您可以像这样禁用 SSL 验证(不推荐用于生产!)

    https://github.com/astro/socksify-ruby/blob/master/test/tc_socksify.rb#L110

    【讨论】:

    • 这是一个正确的方法。文档状态: 1. SSL 标志必须设置之前调用Net::HTTP#start,并开始会话 2. 将 SSL in an option 设置为 Net::HTTP#start 意味着这是一个正确的设置方法
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多