Tag Archives: HTTP

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

Downloading a Web page in C using a Windows socket

/* Link with Ws2_32.lib */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <stdio.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>


int main(void)
{
	WSADATA wsaData;
	WORD wVersion;
	SOCKET sock;
	char host[] = "www.martinbroadhurst.com";
	char port[] = "80";
	struct addrinfo hints, *res;
	char message[] = "GET / HTTP/1.1\nHost: www.martinbroadhurst.com\n\n";
	char buf[1024];
	int bytes_read;
	int status;

	wVersion = MAKEWORD(1, 1);
	if (WSAStartup(wVersion, &wsaData) != 0) {
		printf("Couldn't initialise WinSock\n");
		return 1;
	}

	ZeroMemory(&hints, sizeof hints);
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	status = getaddrinfo(host, port, &hints, &res);
	if (status != 0) {
		perror("getaddrinfo");
        WSACleanup();
		return 1;
	}
	sock = socket(AF_INET, SOCK_STREAM, 0);

	status = connect(sock, res->ai_addr, res->ai_addrlen);
	if (status == SOCKET_ERROR) {
		perror("connect");
        WSACleanup();
		return 1;
	}

	send(sock, message, sizeof(message), 0);

    do {
		bytes_read = recv(sock, buf, sizeof(buf), 0);
		if (bytes_read == SOCKET_ERROR) {
			perror("recv");
		}
		else {
			printf("%.*s", bytes_read, buf);
		}
	} while (bytes_read > 0);

	closesocket(sock);
	WSACleanup();
    return 0;
}

Reference: Using Winsock

Downloading a Web page in C using a socket

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close() */
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
    int sock;
	char host[] = "www.martinbroadhurst.com";
	char port[] = "80";
	struct addrinfo hints, *res;
	char message[] = "GET / HTTP/1.1\nHost: www.martinbroadhurst.com\n\n";
	unsigned int i;
    char buf[1024];
    int bytes_read;
	int status;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	status = getaddrinfo(host, port, &hints, &res);
	if (status != 0) {
		perror("getaddrinfo");
		return 1;
	}
	sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if (sock == -1) {
		perror("socket");
		return 1;
	}
	status = connect(sock, res->ai_addr, res->ai_addrlen);
    if (status == -1) {
		perror("connect");
		return 1;
    }
	freeaddrinfo(res);
    send(sock, message, strlen(message), 0);

    do {
		bytes_read = recv(sock, buf, 1024, 0);
		if (bytes_read == -1) {
			perror("recv");
		}
        else {
		    printf("%.*s", bytes_read, buf);
        }
    } while (bytes_read > 0);

    close(sock);

    return 0;
}