加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何通过代理运行Web请求时设置use_ssl参数?

发布时间:2020-12-17 02:09:30 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 4.2.7.如何通过代理发送Web请求时设置“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.g
我正在使用Rails 4.2.7.如何通过代理发送Web请求时设置“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>'

解决方法

根据这个测试样本,第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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读