2010-11-07 94 views
3

我需要能夠發送一個HTTP GET請求,要求客戶提供特定的證書進行身份驗證的Web服務。 .net代碼看起來像這樣:我如何可以驗證具有證書的HTTP web請求?

if (certificate != null) 
    request.ClientCertificates.Add(certificate); 
return request; 

我還沒有能夠找出軌道中的等價物。有什麼建議麼?

回答

2

如果使用基本的網絡/ HTTPS,那麼它很簡單:

require 'net/https' 
require 'uri' 

uri = URI.parse(ARGV[0] || 'https://localhost/') 
http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS 
http.key = pkey #Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object. 
http.cert= cert #Sets an OpenSSL::X509::Certificate object as client certificate 

http.start { 
    http.request_get(uri.path) {|res| 
    print res.body 
    } 
} 

如果你讓他們結合起來,你就必須使用一定的OpenSSL實用的方法來加以按摩。

對於自定義HTTP客戶端,你應該閱讀紅寶石OpenSSL庫的文檔的血淋淋的細節。

但概括地說,這樣的事情應該工作:

ctx = OpenSSL::SSL::SSLContext.new 
ctx.key = private_key_file 
ctx.cert = certificate_file 

..和然後提供上下文的連接。