/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>

#include <windows.h>
#include <wininet.h>

int main()
{
    HINTERNET inet, con, req;
    DWORD len;
    BYTE hdr[10000], body[10000];
    LPCTSTR acctyp[] = { "*/*" };
    DWORD hdrcod[] = { HTTP_QUERY_STATUS_CODE, HTTP_QUERY_STATUS_TEXT, HTTP_QUERY_CONTENT_LENGTH, HTTP_QUERY_CONTENT_TYPE };
    char *hdrnam[] = { "Status-Code", "Status-Text", "Length",  "Content-Type" };
    int i;
    inet = InternetOpen("WinInet", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if(inet == NULL) 
    {
        printf("InternetOpen failed\n");
        exit(1);
    }
    con = InternetConnect(inet, "localhost", 81, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if(con == NULL)
    {
        printf("InternetConnect failed\n");
        exit(1);
    }
    req = HttpOpenRequest(con, "GET", "/", "1.0", NULL, acctyp, 0, 0);
    if(req == NULL)
    {
        printf("HttpOpenRequest failed\n");
        exit(1);
    }
    if(!HttpSendRequest(req, NULL, 0, NULL, 0)) 
    {
        printf("HttpSendRequest failed\n");
        exit(1);
    }
    for(i = 0; i < 4; i++)
    {
        len = sizeof(hdr);
        if(HttpQueryInfo(req, hdrcod[i], hdr, &len, NULL))
        {
            hdr[len] = '\0';
            printf("%s = %s\n", hdrnam[i], hdr);
        }
        else
        {
            printf("HttpQueryInfo failed\n");
            exit(1);
        }
    }
    if(InternetReadFile(req, body, sizeof(body), &len))
    {
        body[len] = '\0';
        printf("%s\n", body);
    }
    else
    {
        printf("ReadInternetFile failed\n");
        exit(1);
    }
    InternetCloseHandle(inet);
    return 0;
}
