3

I need to sent a HTTP GET Request to my site and then get the contents of the page and parse it. I'de prefer to not use libraries like libCURL because I want the end application to be fully independent (This is quoting from personal experience, I may be wrong but if I recall the client has to have certain Dynamic Link Libraries to run applications with libCURL libraries.), I'm using C++ in Visual Studio 2012.

Code examples would also be good.

Ryan
  • 957
  • 5
  • 16
  • 34
  • Are you asking how to use winsock2 ? – nurettin Dec 22 '12 at 16:36
  • Open a socket(), send() your request, recv() your answer.. What have you tried already? Where are you stuck? How can we help you? – cmc Dec 22 '12 at 16:38
  • 4
    You can always statically link libcurl instead of dynamically linking it if you don't want to deal with distributing DLLs. – Adam Rosenfield Dec 22 '12 at 16:48
  • You want to use libCurl. Doing HTTP request may seem trivial if it works but if you diverge from normal by any amount the shit starts to happen very quickly. – Martin York Dec 22 '12 at 18:46

3 Answers3

8

When you don't want to use an external library, you will have to implement HTTP yourself. When you only need the basic functionality (direct download of a file, no redirects, no proxies, no cookies, no authentication, no encryption, no compression, no other shenanigans), this isn't even that hard.

Create a socket, connect it to port 80 of your webserver, and send the following strings to the server:

"GET /example.html HTTP/1.1\r\n"
"Host: www.example.com\r\n"
"\r\n"

This requests the file www.example.com/example.html from the server you connected to.

The server will respond with an own HTTP response header followed by the data (or an error description).

Philipp
  • 67,764
  • 9
  • 118
  • 153
3
  1. Most libraries can be compiled statically. When you use a static library it gets linked in into your executable and you do not have to ship the library's dll. Maybe this would help: Walkthrough: Creating and Using a Static Library (C++)
  2. Some libraries can be header-only or used in a header-only mode: compiler pulls the library into your source. Again, no dll is generated for the library. For example, you can try getting Asio and cpp-netlib to work in a header-only mode for you. There is also a sampe of a simple http client just for the Asio lying somewhere.
  3. I think you can use WinINet or WinHTTP without shipping any DLLs.
Paul
  • 181
  • 4
  • 11
ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • 2
    The WinInet/WinHTTP DLLs are already installed in the OS. You do not need to deploy the DLLs yourself, but they are still DLLs nontheless. – Remy Lebeau Dec 22 '12 at 18:39
0

Here is a simple code for a GET request in C++

std::string GET(std::string url) {
    std::string cmd = "curl -s " + url;
    FILE *p = popen(cmd.c_str(), "r");
    char buffer[2];
    std::string body;
    if (!p) {
        return "Error";
    }
    while (fgets(buffer, sizeof(buffer), p)) {
        body += buffer;
    }
    return body;
}

Using:

std::string response = GET("https://example.com");
// Do actions with response

Good luck!

flow
  • 13
  • 5
  • 1
    Useful, but note that this doesn't make an HTTP request in C++ as advertised, it runs `curl` as subprocess which makes the HTTP request. OP says: "I'd prefer to not use libraries like libCURL because I want the end application to be fully independent". – ggorlen Mar 09 '23 at 02:35