ash-breeze
ash-breeze

Reputation: 458

Sending an http get request to a website, ignoring the response with c++

I need to send a http get request using c++. My code as of now is:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main ()
{
    ifstream llfile;
    llfile.open("C:/AdobeRenderServerLog.txt");

    if(!llfile.is_open()){
        exit(EXIT_FAILURE);
    }

    char word[50];
    llfile >> word;
    cout << word;
    llfile.close();
    return 0;
}

The request would be something sent to:

www.example.com/logger.php?data=word

Upvotes: 2

Views: 1151

Answers (1)

gbjbaanb
gbjbaanb

Reputation: 52679

Possibly the easiest is to use libCurl.

Using the 'easy interface' you just call curl_easy_init(), then curl_easy_setopt() to set the url, then curl_easy_perform() to make it do the getting. If you want the response (or progress, etc) then set the appropriate properties in setopt(). Once you're all done, call curl_easy_cleanup(). Job done!

The documentation is comprehensive - it's not just a simple lib to get http requests, but does practically every network protocol. Realise that the doc therefore looks pretty complicated, but it isn't really.

It might be an idea to go straight to the example code, the simple one looks like this:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

but you might want to check out the 'get a file in memory' sample or the 'replace fopen' one as well.

Upvotes: 1

Related Questions