【发布时间】:2016-02-28 02:09:39
【问题描述】:
我正在尝试使用 R 中的 HTTP POST 方法从我的服务器获取 JSON 数组。
我尝试使用来自httr 的POST函数和来自RCurl 的getURL函数,但都返回错误。
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")...您需要获取信任库文件。