Tengo este codigo:
#include <stdio.h>
#include "curl/include/curl/curl.h"
size_t dataSize=0;
size_t curlWriteFunction(void* ptr, size_t size/*always==1*/,
size_t nmemb, void* userdata)
{
char** stringToWrite=(char**)userdata;
const char* input=(const char*)ptr;
if(nmemb==0) return 0;
if(!*stringToWrite)
*stringToWrite=malloc(nmemb+1);
else
*stringToWrite=realloc(*stringToWrite, dataSize+nmemb+1);
memcpy(*stringToWrite+dataSize, input, nmemb);
dataSize+=nmemb;
(*stringToWrite)[dataSize]='\0';
return nmemb;
}
int main()
{
char* data=0;
CURL*const curl=curl_easy_init();
if(!curl)
{
fprintf(stderr, "error: Initialization");
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteFunction);
if(curl_easy_perform(curl)!=CURLE_OK)
{
fprintf(stderr, "error: get web page\n");
return 1;
}
curl_easy_cleanup(curl);
if(!data)
{
fprintf(stderr, "error: Got no data\n");
return 1;
}
printf("Page data:\n\n%s\n", data);
free(data);
}
En Linux, al especificar https: en el URL, no hace ninguna diferencia, sin embargo cuando lo hago en Windows falla la funcion curl_easy_perform. ¿Alguien tiene idea de por que?
Gracias y saludos,
marax.
que error te da? puede ser que te haga falta openssl o algo similar
Hola, gracias por responder.
CitarC:\Users\Programming\Desktop\CURL test>main https://www.google.com
Failed to get web page: SSL peer certificate or SSH remote key was not OK
C:\Users\Programming\Desktop\CURL test>
pareciera un error con el certificado ssl el asunto es que no se si remoto o local, estás tras proxy, vpn o similar?
intenta con la opcion CURLOPT_VERBOSE activada a ver si te da más info que sea util para determinar el problema
CitarC:\Users\Programming\Desktop\CURL test>main https://www.google.com
* Trying 142.250.185.4:443...
* Connected to www.google.com (142.250.185.4) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
Failed to get web page: SSL peer certificate or SSH remote key was not OK
C:\Users\Programming\Desktop\CURL test>
¿Que es "Certificado del emisor"?
Citar
Download the latest cacert.pem from https://curl.haxx.se/ca/cacert.pem
Add the following line to php.ini: (if this is shared hosting and you don't have access to php.ini then you could add this to .user.ini in public_html).
curl.cainfo="/path/to/downloaded/cacert.pem"
Make sure you enclose the path within double quotation marks!!!
CitarI found out you need to download the .pem file here https://curl.haxx.se/docs/caextract.html
And add these settings...
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, "PATH TO FILE\\cacert.pem");
curl_easy_setopt(curl, CURLOPT_CAPATH, "PATH TO FILE\\cacert.pem");
por aclarar un poco
Cita de: marax en 16 Mayo 2021, 21:43 PM
¿Que es "Certificado del emisor"?
el SSL no es unidireccional, es decir, no es que la pagina te pasa su certificado y listo, sino que es bidireccional, ella te pasa un certificado y tu le pasas uno, ambos son requeridos para el cifrado asimetrico....
El certificado del emisor, es
tu certificado, el curl en tu windows aparentemente no lo consigue y por lo tanto no puede terminar el saludo SSL (handshake)
lo que propone el-brujo, es la solución oficial, que te descargues el certificado oficial de curl (cacert.pem) y que lo incluyas en el código (CURLOPT_CAINFO y CURLOPT_CAPATH)
Hola,
gracias por su atencion, pero el problema inicial persiste:
CitarC:\Users\Programming\Desktop\CURL test>main https://www.google.com/
* Trying 142.250.185.4:443...
* Connected to www.google.com (142.250.185.4) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: cacert.pem
* CApath: cacert.pem
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=www.google.com
* start date: May 3 11:24:19 2021 GMT
* expire date: Jul 26 11:24:18 2021 GMT
* subjectAltName: host "www.google.com" matched cert's "www.google.com"
* issuer: C=US; O=Google Trust Services; CN=GTS CA 1O1
* SSL certificate verify ok.
* No OCSP response received
* Closing connection 0
Failed to get web page: SSL server certificate status verification FAILED
C:\Users\Programming\Desktop\CURL test>
Cita de: marax en 21 Mayo 2021, 19:29 PM
Hola,
gracias por su atencion, pero el problema inicial persiste:
No uses:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1);
Los servidores de google (o al menos al que estás intentando conectarte) no soportan esa extensión.
Edit:
¿O quizás es algo que te haga falta a ti?
Tienes razon, MinusFour. Ese era el problema.
Ahora funciona.
Gracias por su atencion.