HTTP with WinINet
This chapter covers a minimal HTTP GET using WinINet (wininet.dll), plus readable errors via FormatMessageW pulling messages from wininet.dll.
Link Wininet.lib and include Wininet.h.
Flow in main
Typical sequence:
InitSession—InternetOpenAwith a user agent andINTERNET_OPEN_TYPE_PRECONFIG.MakeRequest—InternetConnectA, thenHttpOpenRequestA(and related) to obtain anHINTERNETrequest handle.SendRequest— send headers/body; on failure, call your error helper.ProcessResults—InternetReadFile(or similar) in a loop until done.CloseSession—InternetCloseHandlefor each open handle (request, connection, session).
Split these across .c files in your template (HttpApis.c, Errors.c, etc.) if your rubric asks for separation—the book describes the pattern; your repo holds the actual file names.
Formatting WinINet errors
#define BUFFER_SIZE 1024
void CheckLastInetError(LPCWSTR message, DWORD dwLastErr)
{
WCHAR buf[BUFFER_SIZE];
DWORD n = FormatMessageW(
FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandleW(L"wininet.dll"),
dwLastErr,
0UL,
buf,
BUFFER_SIZE,
NULL
);
if (n)
wprintf(L"%s: %s", message, buf);
}
Tip
Distinguish Win32 errors from
InternetError/ extended WinINet codes your debugger shows—always threadGetLastError(or the API’s documented error path) intoFormatMessagewith the right module when possible.
Note
Samples often target
127.0.0.1:8080. Run a local listener or change host / port to match your lab.
Implement
- Add WinINet session/connect/request/send/read/close in your template.
- Wire
CheckLastInetError(or equivalent) for failed steps. - Build, test against a real or mock server, commit, and push.