Skip to content

Instantly share code, notes, and snippets.

@supersuryaansh
Last active December 26, 2023 16:07
Show Gist options
  • Save supersuryaansh/9b7a508ada88ed28829f8e5a9ad44db4 to your computer and use it in GitHub Desktop.
Save supersuryaansh/9b7a508ada88ed28829f8e5a9ad44db4 to your computer and use it in GitHub Desktop.
Return status code of a webpage in C using CURL
// requires #include <curl/curl.h>
int statusCode(char *addr){
CURL *curl;
CURLcode res;
long response_code;
// Initialize curl
curl_global_init(CURL_GLOBAL_DEFAULT);
// Create a curl handle
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, addr);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
//set server user/pass for basic auth
curl_easy_setopt(curl, CURLOPT_USERPWD, "supersu:guideme");
res = curl_easy_perform(curl);
if(res == CURLE_OK) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
// Cleanup global curl resources
curl_global_cleanup();
return response_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment