[英]lib curl in c++ disable printing
i got a small program from http://curl.haxx.se/ and while i run it always prints the webpage how can i disable the printing function 我从http://curl.haxx.se/得到一个小程序,而我运行它总是打印网页如何禁用打印功能
#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;
}
You need to set up a CURLOPT_WRITEFUNCTION to make it not use stdout. 您需要设置CURLOPT_WRITEFUNCTION以使其不使用stdout。
There is an explanation here (under CURLOPT_WRITEFUNCTION): http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 这里有一个解释(在CURLOPT_WRITEFUNCTION下): http : //curl.haxx.se/libcurl/c/curl_easy_setopt.html
and here (Under "Handling the Easy libcurl): http://curl.haxx.se/libcurl/c/libcurl-tutorial.html 在这里(在“处理简单的libcurl”下): http : //curl.haxx.se/libcurl/c/libcurl-tutorial.html
Basically adding the function: 基本上添加功能:
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
return size * nmemb;
}
and calling 并打电话
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
Should do it. 应该这样做。
You can still get diagnostic messages. 您仍然可以获得诊断消息。 To stop these either change or add the following line:
要停止这些更改或添加以下行:
curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L); //0 disable messages
To write data into file instead of printing, give a file descriptor as: 要将数据写入文件而不是打印,请将文件描述符指定为:
FILE *wfd = fopen("foo.txt", "w");
...
curl_easy_setopt(curl, CURLOPT_WRITEDATA, wfd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
What worked for me was using the CURLOPT_NOBODY option in the code, referenced here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOBODY 对我有用的是在代码中使用CURLOPT_NOBODY选项,在此引用: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.