Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:09:41+00:00 2026-06-04T20:09:41+00:00

In C++, I want to use libcurl to check that a URL is text/html,

  • 0

In C++, I want to use libcurl to check that a URL is text/html, and if yes, to then download the body, else it stops.

I want this todo in one step, not first sending HEAD, if HEAD is ok, requesting the page again to download.

If this is not possible with libcurl, does it give other libaries for C++, which would support this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:09:43+00:00Added an answer on June 4, 2026 at 8:09 pm

    To send a HTTP HEAD request, you need to set this option up:

     curl_easy_setopt(ctx,CURLOPT_NOBODY ,1 );
    

    and you can also have a look at this question: help needed on libcurl programming in sending HTTP HEAD Request

    to separate headers from body (don’t know if it is really needed), you can have a look at :

    http://curl.haxx.se/libcurl/c/sepheaders.html

    to download lot of different URLs libcurl-multi, you should have a look at :

    http://curl.haxx.se/libcurl/c/libcurl-multi.html

    slight modification of http://curl.haxx.se/libcurl/c/sendrecv.html:

    #include <stdio.h>
    #include <string.h>
    #include <curl/curl.h>
    
    /* Auxiliary function that waits on the socket. */
    static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
    {
      struct timeval tv;
      fd_set infd, outfd, errfd;
      int res;
    
      tv.tv_sec = timeout_ms / 1000;
      tv.tv_usec= (timeout_ms % 1000) * 1000;
    
      FD_ZERO(&infd);
      FD_ZERO(&outfd);
      FD_ZERO(&errfd);
    
      FD_SET(sockfd, &errfd); /* always check for error */
    
      if(for_recv)
      {
        FD_SET(sockfd, &infd);
      }
      else
      {
        FD_SET(sockfd, &outfd);
      }
    
      /* select() returns the number of signalled sockets or -1 */
      res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
      return res;
    }
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
      /* Minimalistic http request */
      const char *request = "GET / HTTP/1.0\r\nHost: m0g.net\r\n\r\n";
      curl_socket_t sockfd; /* socket */
      long sockextr;
      size_t iolen;
      curl_off_t nread;
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://m0g.net");
        /* Do not do the transfer - only connect to host */
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
        res = curl_easy_perform(curl);
    
        if(CURLE_OK != res)
        {
          printf("Error: %s\n", strerror(res));
          return 1;
        }
    
        /* Extract the socket from the curl handle - we'll need it for waiting.
         * Note that this API takes a pointer to a 'long' while we use
         * curl_socket_t for sockets otherwise.
         */
        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
    
        if(CURLE_OK != res)
        {
          printf("Error: %s\n", curl_easy_strerror(res));
          return 1;
        }
    
        sockfd = sockextr;
    
        /* wait for the socket to become ready for sending */
        if(!wait_on_socket(sockfd, 0, 60000L))
        {
          printf("Error: timeout.\n");
          return 1;
        }
    
        puts("Sending request.");
        /* Send the request. Real applications should check the iolen
         * to see if all the request has been sent */
        res = curl_easy_send(curl, request, strlen(request), &iolen);
    
        if(CURLE_OK != res)
        {
          printf("Error: %s\n", curl_easy_strerror(res));
          return 1;
        }
        puts("Reading response.");
        char data[2048];
        int idxread=0;
    
        /* read the response */
        for(;;)
        {
          char buf[1024];
    
          wait_on_socket(sockfd, 1, 60000L);
          res = curl_easy_recv(curl, buf, 32, &iolen);
    
          if(CURLE_OK != res)
            break;
    
          if (nread+idxread > 2048)
              break;
    
          strncpy(data+idxread,buf,nread);
          idxread+=nread;
    
          if (strstr(data,"\r\n\r\n") != NULL) {
            if (strstr(data,"Content-Type: text/html") == NULL) {
                printf("not an html document.");
                return 2;
            }
          }
    
          nread = (curl_off_t)iolen;
    
          printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread);
        }
        printf("'''%s'''\n", data);
    
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to use title on text. is this possible to make title on
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
I want use $.ajax get a url string from a xml file ,then with
I want use this 1 for using Bar code or QR code scanner. I
We have a powerbuilder application and we want use a scanner through this application
I have class in server side and I want use method of this class
I want use a typedef struct that isn't already defined, but it is later.
I need to use HTML comments to store specific data, but I don't want
I want to get a HTML and use like a file in C. Actually
I am writing a command-line tool for Windows that uses libcurl to download files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.