gpt4 book ai didi

c - 使用 libcurl 在 C 中检索数据

转载 作者:行者123 更新时间:2023-12-04 05:26:49 25 4
gpt4 key购买 nike

我正在使用 C 和 libcurl 登录网站并从表单中检索值(即将字符串“username=random”放入字符数组中)。这是我到目前为止:

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, " ");
curl_easy_setopt(curl, CURLOPT_URL, "http://www.website.com/login");
curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_REFERER, "http://www.website.com/login");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,fields );
curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_URL, "http://www.website.com/form-with-data");

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

}

但我不确定从那里去哪里。我试过将整个页面写入一个文件,然后手动搜索字符串,但这不起作用。

我确定对此有一个简单的答案,我只是 C 和 libcurl 的新手:)

最佳答案

您当前的代码将做一件事:它将数据写入标准输出。要累积数据,您必须执行以下操作:

size_t write_clbk(void *data, size_t blksz, size_t nblk, void *ctx)
{
static size_t sz = 0;
size_t currsz = blksz * nblk;

size_t prevsz = sz;
sz += currsz;
void *tmp = realloc(*(char **)ctx, sz);
if (tmp == NULL) {
// handle error
free(*(char **)ctx);
*(char **)ctx = NULL;
return 0;
}
*(char **)ctx = tmp;

memcpy(*(char **)ctx + prevsz, data, currsz);
return currsz;
}

hndl = curl_easy_init();
// Set up the easy handle, i. e. specify URL, user agent, etc.
// Do the ENTIRE setup BEFORE calling `curl_easy_perform()'.
// Afterwards the calls to `curl_easy_setopt()' won't be effective anymore

char *buf = NULL;
curl_easy_setopt(hndl, CURLOPT_WRITEFUNCTION, write_clbk);
curl_easy_setopt(hndl, CURLOPT_WRITEDATA, &buf);
curl_easy_perform(hndl);
curl_easy_cleanup(hndl);

// here `buf' will contain the data
// after use, don't forget:
free(buf);

关于c - 使用 libcurl 在 C 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13116569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com