00001 /*************************************************************************** 00002 * _ _ ____ _ 00003 * Project ___| | | | _ \| | 00004 * / __| | | | |_) | | 00005 * | (__| |_| | _ <| |___ 00006 * \___|\___/|_| \_\_____| 00007 * 00008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. 00009 * 00010 * This software is licensed as described in the file COPYING, which 00011 * you should have received as part of this distribution. The terms 00012 * are also available at https://curl.haxx.se/docs/copyright.html. 00013 * 00014 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 00015 * copies of the Software, and permit persons to whom the Software is 00016 * furnished to do so, under the terms of the COPYING file. 00017 * 00018 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 00019 * KIND, either express or implied. 00020 * 00021 ***************************************************************************/ 00022 /* <DESC> 00023 * Show how to extract Location: header and URL to redirect to. 00024 * </DESC> 00025 */ 00026 #include <stdio.h> 00027 #include <curl/curl.h> 00028 00029 int main(void) 00030 { 00031 CURL *curl; 00032 CURLcode res; 00033 char *location; 00034 long response_code; 00035 00036 curl = curl_easy_init(); 00037 if(curl) { 00038 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); 00039 00040 /* example.com is redirected, figure out the redirection! */ 00041 00042 /* Perform the request, res will get the return code */ 00043 res = curl_easy_perform(curl); 00044 /* Check for errors */ 00045 if(res != CURLE_OK) 00046 fprintf(stderr, "curl_easy_perform() failed: %s\n", 00047 curl_easy_strerror(res)); 00048 else { 00049 res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); 00050 if((res == CURLE_OK) && 00051 ((response_code / 100) != 3)) { 00052 /* a redirect implies a 3xx response code */ 00053 fprintf(stderr, "Not a redirect.\n"); 00054 } 00055 else { 00056 res = curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &location); 00057 00058 if((res == CURLE_OK) && location) { 00059 /* This is the new absolute URL that you could redirect to, even if 00060 * the Location: response header may have been a relative URL. */ 00061 printf("Redirected to: %s\n", location); 00062 } 00063 } 00064 } 00065 00066 /* always cleanup */ 00067 curl_easy_cleanup(curl); 00068 } 00069 return 0; 00070 }