【发布时间】:2018-01-24 21:24:49
【问题描述】:
我正在尝试连接一个通过 SSL 访问某个站点并在该站点上查询我的数据的网络爬虫。本网站的身份验证是通过自签名数字证书进行的。在我想访问该站点的那一刻,我将此 .pfx 格式的证书上传到我的 api,将其转换为 .pem,当我尝试使用此证书访问该站点时,响应带有状态 403(禁止)。
但是,当我尝试通过带有 .pfx 格式证书的浏览器访问该站点时,我通常会得到它。
我已经尝试过使用 Mechanize,并且它工作了一段时间(直到几个月前它才起作用),但随后它开始出现错误:
SSL_connect returned = 1 errno = 0 state = SSLv3 read finished A: sslv3 alert bad certificate
该网站很旧,它不会经常收到更新。
之后我已经尝试使用 net / http lib 并且错误仍然存在,我尝试使用 httprb gem,最后我尝试使用 Faraday。所有尝试都以上面引用的错误或响应状态 == 403 结束。
我该怎么做才能连接?我的脚本有问题吗?它是否缺少我需要通过的任何信息?
代码:
# Faraday customs method:
class FaradayHttp
def with_openssl
system "openssl pkcs12 -in my-certificate-path -out certificate-output-path -nodes -password pass:certificate-password"
def cert_object
OpenSSL::X509::Certificate.new File.read("certificate-output-path")
end
# create PKey
def key_object
OpenSSL::PKey.read File.read("certificate-output-path")
end
faraday = Faraday::Connection.new 'https://example-site.com',
:ssl => {
certificate: cert_object,
private_key: key_object,
version: :SSLv3,
verify: false
}
faraday
end
end
# Controller that try to connect with the ssl server:
agent = FaradayHttp.new.with_openssl
page = agent.get '/login_path'
【问题讨论】:
标签: ruby-on-rails ruby ssl certificate faraday