Downloading a Web page in C using neon

#include <stdio.h>

#include <neon/ne_socket.h>
#include <neon/ne_session.h>
#include <neon/ne_request.h>

int main(void)
{
    ne_sock_init();	 
    ne_session *session = ne_session_create("http",	"www.martinbroadhurst.com", 80);
    ne_request *request = ne_request_create(session, "GET", "/");
    if (ne_begin_request(request) == NE_OK) {
        char buf[1024];
        size_t bytes_read;
        while ((bytes_read = ne_read_response_block(request, buf, 1024)) > 0) {
            printf("%.*s", (int)bytes_read, buf);
        }
        ne_end_request(request);
    }
    else {
        printf("Failed to dispatch request\n");
    }
    ne_request_destroy(request);
    ne_close_connection(session);
    ne_session_destroy(session);
    ne_sock_exit();	 
    return 0;
}

Reference: neon HTTP and WebDAV client library

Downloading a Web page in C using libcurl

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

size_t write(void *ptr, size_t size, size_t nmemb, void *stream)
{
	FILE *fptr = stream;
	return fwrite(ptr, size, nmemb, fptr); 
}	

int main(void)
{
	CURL *curl = curl_easy_init();
	FILE *fptr = fopen("index.html", "w+");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
	curl_easy_setopt(curl, CURLOPT_URL, "http://www.martinbroadhurst.com/");
	curl_easy_perform(curl);
	fclose(fptr);
	curl_easy_cleanup(curl);

	return 0;
}

Reference: libcurl – the multiprotocol file transfer library

Downloading a Web page in C using WinINet


/* Link with Wininet.lib */
#include <windows.h>
#include <wininet.h>
#include <stdio.h>

int main(void)
{
	HINTERNET hInternet, hFile;
	char buf[1024];
	DWORD bytes_read;
	int finished = 0;
	hInternet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	if (hInternet == NULL) {
		printf("InternetOpen failed\n");
		return 1;
	}
	hFile = InternetOpenUrl(hInternet, "http://www.martinbroadhurst.com", NULL, 0L, 0, 0);
	if (hFile == NULL) {
		printf("InternetOpenUrl failed\n");
		return 1;
	}
	while (!finished) {
		if (InternetReadFile(hFile, buf, sizeof(buf), &bytes_read)) {
			if (bytes_read > 0) {
				printf("%.*s", bytes_read, buf);
			}
			else {
				finished = 1;
			}
		}
		else {
			printf("InternetReadFile failed\n");
			finished = 1;
		}
	}
	InternetCloseHandle(hInternet);
	InternetCloseHandle(hFile);
			
	return 0;
}

Reference: InternetOpenUrl function