问题
I'm trying to fetch a JSON array from my server using the HTTP POST method in R.
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)
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
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
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.
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
Any help would be most apreciated
回答1:
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
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]]))
Also try validating it
cert_verify(cert, ca_bundle())
This might give a hint on what's wrong with your certificate.
回答2:
It seems like changing
ServerName www.example.com
To
ServerName example.com
fixed this issue. I tried this solution from another computer and I was able to use the httr POST function with this fix with the default httr CA bundle.
来源:https://stackoverflow.com/questions/33913916/get-site-content-over-ssl-with-httr-in-r