Skip to content

Instantly share code, notes, and snippets.

@emadflash
Created August 24, 2021 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadflash/2b702135c80b9e9c2a51ce960b880780 to your computer and use it in GitHub Desktop.
Save emadflash/2b702135c80b9e9c2a51ce960b880780 to your computer and use it in GitHub Desktop.
Get request with libcurl
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
CURL* xcurl_easy_init() {
CURL* curl = curl_easy_init();
if (!curl) exit(0);
return curl;
}
#define TARGET_URL "https://curl.se/libcurl/c/CURLINFO_RESPONSE_CODE.html"
int main(int argc, char** argv) {
CURL* curl = xcurl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, TARGET_URL);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
char* url;
long response_code;
double elapsed;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
printf("\nURL: %s \nCODE: %d \n\TIME: %d\n", url, response_code, elapsed);
curl_easy_cleanup(curl);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment