2010-08-26 12 views
10

tengo un pequeño programa de http://curl.haxx.se/ y mientras corro siempre imprime la página web ¿Cómo puedo desactivar la función de impresiónrizo lib en C++ desactivar la impresión

#include <iostream> 
#include <curl/curl.h> 
using namespace std; 

int main() { 
    CURL *curl; 
     CURLcode res; 

     curl = curl_easy_init(); 
     if(curl) { 
     curl_easy_setopt(curl, CURLOPT_URL, "http://google.com"); 
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); 
     res = curl_easy_perform(curl); 

     /* always cleanup */ 
     curl_easy_cleanup(curl); 
     } 
     return 0; 
} 

Respuesta

16

Usted necesidad de establecer un CURLOPT_WRITEFUNCTION para que no use stdout.

Hay una explicación aquí (en CURLOPT_WRITEFUNCTION): http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

y aquí (Bajo "Manejo de la libcurl Fácil): http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

añadiendo Básicamente la función:

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) 
{ 
    return size * nmemb; 
} 

y llamando al

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 

Debería hacerlo.

-2

Lo que funcionó para mí estaba usando la opción CURLOPT_NOBODY en el código, hace referencia aquí: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOBODY

#include <iostream> 
#include <curl/curl.h> 
using namespace std; 

int main() { 
    CURL *curl; 
    CURLcode res; 

    curl = curl_easy_init(); 
    if(curl) { 
     curl_easy_setopt(curl, CURLOPT_URL, "http://google.com"); 
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); 

     //USING CURLOPT NOBODY 
     curl_easy_setopt(curl, CURLOPT_NOBODY,1); 

     res = curl_easy_perform(curl); 

     /* always cleanup */ 
     curl_easy_cleanup(curl); 
    } 
    return 0; 
} 
+0

Seguido esto porque parecía más fácil que la otra solución, pero la [Documentación] (https://curl.haxx.se/libcurl/c/CURLOPT_NOBODY.html) dice explícitamente que 'para HTTP (S), esto hace que libcurl haga una solicitud HEAD, lo que generalmente cambia mucho el comportamiento del programa. –

1

todavía puede obtener mensajes de diagnóstico. Para detener estos cambiar o añadir la siguiente línea:

curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L); //0 disable messages