gpt4 book ai didi

c - [curl lib]如何获得响应/重定向 url?

转载 作者:太空狗 更新时间:2023-10-29 15:22:18 47 4
gpt4 key购买 nike

如果我访问 http://www.microsoft.com/将重定向到 http://www.microsoft.com/en-us/default.aspx

如何使用 CURL 库获取响应/重定向 url?

我试试curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &reUrl);这将得到 http://www.microsoft.com/

curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &reUrl);这将始终为 NULL

谢谢你的帮助

最佳答案

设置CURLOPT_FOLLOWLOCATION1

#include <stdio.h>
#include <curl/curl.h>

int main(int argc, char** argv)
{
CURL *curl;
CURLcode curl_res;

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();

if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://www.microsoft.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

/* Perform the request, curl_res will get the return code */
curl_res = curl_easy_perform(curl);

/* Check for errors */
if(curl_res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(curl_res));

if(CURLE_OK == curl_res)
{
char *url;
curl_res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);

if((CURLE_OK == curl_res) && url)
printf("CURLINFO_EFFECTIVE_URL: %s\n", url);
}

/* always cleanup */
curl_easy_cleanup(curl);

/* we're done with libcurl, so clean it up */
curl_global_cleanup();

}
else
{
printf("cURL error.\n");
}

return 0;
}

你会看到:

CURLINFO_EFFECTIVE_URL: http://www.microsoft.com/en-us/default.aspx

关于c - [curl lib]如何获得响应/重定向 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16277894/

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