在RoR中,如何通过socks端口发送https请求?

时间:2016-09-19 14:37:52

标签: ruby-on-rails ruby https socks net-http

我正在使用以下代码通过SOCKS检索网页

  uri = URI(url)
  content = nil
  status = nil
  content_type = nil
  res1 = Net::HTTP.SOCKSProxy('127.0.0.1', 50001).start(uri.host, uri.port) do |http|
    puts "launching #{uri.path}"
    resp = http.get(uri.path)
    status = resp.code
    content = resp.body
    content_type = resp['content-type']
  end

事先,我不知道网址是“http”还是“https”。当我尝试连接到https端口时,看起来请求仍然通过http发送,如下所示......

doc = WebpageHelper.get_url("https://whatismyip.com/")

实际上导致了

2.3.0 :013 >   doc.to_s
 => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html>\r\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<title>400 The plain HTTP request was sent to HTTPS port</title>\n</head>\r\n<body bgcolor=\"white\">\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>The plain HTTP request was sent to HTTPS port</center>\r\n<hr>\n<center>cloudflare-nginx</center>\r\n</body>\r\n</html>\n"

我是否需要在上面设置一些可以通过https强制请求的内容,如果它实际上是一个https网页?

0 个答案:

没有答案