在 R 中使用 httr 通过 SSL 获取站点内容 [英] Get site content over SSL with httr in R

查看:25
本文介绍了在 R 中使用 httr 通过 SSL 获取站点内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to fetch a JSON array from my server using the HTTP POST method in R.

我尝试使用 httr 中的 POST 函数和 RCurl 中的 getURL 函数,但两者都使用返回错误.

I've tried using both the POSTfunction from httrand the getURLfunction from RCurl but both return errors.

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 函数给出的错误是(对于两个调用):

The error given by the POST function is (for both calls):

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

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

The error given by the getURL function is (without 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 运行有关:

I'm suspecting this has something to do with R since running:

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

从命令行返回预期结果.

from the command line return the expected result.

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

The server is a apache2 server with COMODO SSL certificate. In /etc/apache2/sites-enabled/000-default.conf the server name is set to

ServerName www.example.com  

非常感谢任何帮助

推荐答案

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

The httr package includes it's own CA bundle so this probably not the issue. More likely a server side SNI config problem or a problem with your certificate

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

Unfortunately you haven't posted a reproducible example with an actual URL. But with the latest version of the new openssl package you can easily debug your server cert:

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

也尝试验证它

cert_verify(cert, ca_bundle())

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

This might give a hint on what's wrong with your certificate.

这篇关于在 R 中使用 httr 通过 SSL 获取站点内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆