Ruby SSL_read:tlsv1警报协议版本

时间:2016-08-26 06:49:32

标签: ruby ssl https client

我正在编写一个简单的ssl Web客户端,在没有添加ssl支持的情况下工作正常。现在我已经添加了ssl功能来获取请求,我收到此错误消息:

  

SSL_read:tlsv1 alert protocol version

在谷歌搜索之后,我得出结论来检查openssl版本,我可以看到它支持TLSv1.2。我甚至创建了一个上下文来显式强制tlsv1.2但仍然是相同的错误消息。我的openssl版本:

ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION' ===> OpenSSL 1.0.1f 6 Jan 2014

我的代码:

    require 'socket'
    require 'openssl'

    puts "Please select:"
    puts "get ==> to use GET method"
    puts "post ==> to use POST method"
    method = gets.chomp.downcase

    case method
    when "get"
      print "Please enter the domain name to get the http: "
      domain = "www." << gets.chomp
      print "Now enter the page you want to get: "
      page = gets.chomp

      # opening socket
      soc = TCPSocket.new domain, 443
      ctx = OpenSSL::SSL::SSLContext.new
      ctx.ssl_version = :TLSv1_2
      ssl = OpenSSL::SSL::SSLSocket.new(soc, ctx)
      ssl.sync_close = true
      ssl.connect
      soc.puts "GET #{page} HTTP/1.1\nHost: #{domain}\n\n\n"
      while line = ssl.gets
        puts line
      end
    when "post"
      print "Please enter the domain name to post the http:"
      domain = gets.chomp.downcase
      print "Now enter the post form page:"
      form_page = gets.chomp.downcase
      puts "Please enter the parameters of POST and related values one at a time:"
      puts "(\"done\" to end input)"
      post_variable = []
      while true
        print "Parameter:"
        parameter = gets.chomp.downcase
        break if parameter == "done"
        print "Value:"
        value = gets.chomp.downcase
        break if  value == "done"
        post_variable.push("#{parameter}=#{value}")
        post_sentense = post_variable.join("&")
      end
      # opening socket
      soc = TCPSocket.new domain, 80
      soc.puts "POST #{form_page} HTTP/1.1\nHost: #{domain}\nContent-Length: #{post_sentense.length}\n\n\n#{post_variable}"
      while line = soc.gets
        puts line
      end
    else
      puts "wrong input!!!"
    end

我应该提一下,域名应该输入没有www,页面通常是/index.html。 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题。这一行:

soc.puts "GET #{page} HTTP/1.1\nHost: #{domain}\n\n\n"

应该是这样的:

ssl.puts "GET #{page} HTTP/1.1\nHost: #{domain}\n\n\n"

我不知道为什么,但我认为通过将消息发送到socket而不是ssl,它参与了ssl协商并且没有让协商来完成该过程。