2012-10-02 65 views
4

有沒有辦法在ruby中檢查HTTPS狀態碼?我知道有些方法可以使用require 'net/http'在HTTP中執行此操作,但我正在尋找HTTPS。也許我需要使用不同的庫?檢查https狀態碼ruby

+1

你可以在網上做/ HTTP:http://www.rubyinside.com/how-to-cure-nethttps-risky-default-https-behavior-4010.html –

回答

6

您可以使用淨周圍任何包裝:: HTTP( S)獲得更簡單的行爲。 我用法拉第這裏(https://github.com/lostisland/faraday),但HTTParty具有幾乎相同的功能(https://github.com/jnunemaker/httparty

require 'faraday' 

res = Faraday.get("https://www.example.com/") 
res.status # => 200 

res = Faraday.get("http://www.example.com/") 
res.status # => 200 

(作爲獎勵,你得到選項解析響應,提高狀態異常,記錄請求....

connection = Faraday.new("https://www.example.com/") do |conn| 
    # url-encode the body if given as a hash 
    conn.request :url_encoded 
    # add an authorization header 
    conn.request :oauth2, 'TOKEN' 
    # use JSON to convert the response into a hash 
    conn.response :json, :content_type => /\bjson$/ 
    # ... 
    conn.adapter Faraday.default_adapter 
end 

connection.get("/") 

    # GET https://www.example.com/some/path?query=string 
connection.get("/some/path", :query => "string") 

# POST, PUT, DELETE, PATCH.... 
connection.post("/some/other/path", :these => "fields", :will => "be converted to a request string in the body"} 

# add any number of headers. in this example "Accept-Language: en-US" 
connection.get("/some/path", nil, :accept_language => "en-US") 
+0

感謝@艾倫-W-史密斯編輯。 – rewritten

2
require 'uri' 
require 'net/http' 

res = Net::HTTP.get_response(URI('http://www.example.com/index.html')) 
puts res.code # -> '200'