【问题标题】:Get site content over SSL with httr in R在 R 中使用 httr 通过 SSL 获取站点内容
【发布时间】:2016-02-28 02:09:39
【问题描述】:

我正在尝试使用 R 中的 HTTP POST 方法从我的服务器获取 JSON 数组。

我尝试使用来自httrPOST函数和来自RCurlgetURL函数,但都返回错误。

cafile <- system.file("CurlSSL", "cacert.pem", package = "RCurl")
url    <- "https://example.com/query/getData.php"

POST(url,body=NULL)
POST(url,body=NULL,config(cainfo=cafile))

getURL(url)
getURL(url,cainfo=cafile)

POST 函数给出的错误是(对于两个调用):

Error in curl::curl_fetch_memory(url, handle = handle) : 
  SSL peer certificate or SSH remote key was not OK

getURL函数给出的错误是(没有config(cainfo=cafile)):

* Hostname was NOT found in DNS cache
*   Trying 162.xxx.xxx.xxx...
* connect to 162.xxx.xxx.xxx port 443 failed: Connection refused
*   Trying 130.yyy.yyy.yyy...
* Connected to example.com (130.yyy.yyy.yyy) port 443 (#0)
* found 175 certificates in /etc/ssl/certs/ca-certificates.crt
* gnutls_handshake() warning: The server name sent was not recognized
* failed to get server cert
* Closing connection 0
Error in function (type, msg, asError = TRUE)  : 
  gnutls_handshake() warning: The server name sent was not recognized

自运行以来我怀疑这与 R 有关:

curl 'https://example.com/query/getData.php'

从命令行返回预期结果。

服务器是一个带有 COMODO SSL 证书的 apache2 服务器。 在/etc/apache2/sites-enabled/000-default.conf 中,服务器名称设置为

ServerName www.example.com  

任何帮助将不胜感激

【问题讨论】:

  • 我的猜测是 R 不信任来自 example.com 的证书。要解决此问题,您需要将此证书添加到您的信任库中。
  • 如果在网络浏览器中浏览网站时证书是受信任的,R 不应该信任证书吗?我也在线验证了证书?信任库 R 是特定的吗?
  • 是的,我期待这个。您可以尝试手动将 base 64 编码证书添加到您的 cacert.pem 文件中吗?是的,这是一个 hack,但它会让我们看到发生了什么。
  • 我已经尝试过了,但我遇到了同样的错误,(我没有 .pem 文件,但我有 .ca-bundle、.crt、.p7b .csr 和 .key文件,我都试过了)。我尝试了将详细选项设置为 true 的相同命令,因此我将编辑我的问题以包含此内容。
  • download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem") ...您需要获取信任库文件。

标签: r ssl apache2 rcurl httr


【解决方案1】:

httr 包包含它自己的 CA 包,所以这可能不是问题。更有可能是服务器端 SNI 配置问题或您的证书有问题

很遗憾,您没有发布带有实际 URL 的可重现示例。但是使用最新版本的新 openssl 包,您可以轻松调试服务器证书:

library(openssl)
cert <- download_ssl_cert("www.r-project.org")
print(cert)
print(as.list(cert[[1]]))

也尝试验证它

cert_verify(cert, ca_bundle())

这可能会提示您的证书有什么问题。

【讨论】:

  • 感谢您的帮助,虽然我不想透露我的服务器的域名。看来这是服务器设置的问题。
【解决方案2】:

好像变了

ServerName www.example.com

ServerName example.com

修复了这个问题。我从另一台计算机上尝试了这个解决方案,并且我能够使用带有默认 httr CA 捆绑包的 httr POST 功能和此修复程序。

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多