if I visit http://www.microsoft.com/ will redirect to http://www.microsoft.com/en-us/default.aspx
How can I get response/redirect url using CURL lib ?
I try curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &reUrl); this will get http://www.microsoft.com/
curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &reUrl); this will always get NULL
So thanks for help
Set CURLOPT_FOLLOWLOCATION to 1
#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;
}
You will see:
CURLINFO_EFFECTIVE_URL: http://www.microsoft.com/en-us/default.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With