c++ curl 读取网页代码

180it 2021-06-29 PM 1132℃ 0条
#include<iostream>
#include<string>
#include<curl\curl.h>
using namespace std;

 
static size_t downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer)
{
    string* psResponse = (string*) writer;
    size_t size = sz * nmemb;
    psResponse->append((char*) buffer, size);
 
    return sz * nmemb;
}
 
int main()
{
    
    
    system("chcp 65001");
    system("cls");
    
    string strUrl = "http://www.txttool.com";
    string strTmpStr;
    CURL *curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    string strRsp;
    if (res != CURLE_OK)
    {
        strRsp = "error";
    }
    else
    {
        strRsp = strTmpStr;
    }
    
    cout<<strRsp.c_str()<<endl;
    
//    printf("strRsp is |%s|\n", strRsp.c_str());
    return 0;
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

c++ curl 读取网页代码