#include <WinInet.h>
#pragma comment (lib, "WinInet.lib")
enum RequestType
{
GET,
POST
};
char *SendRequest(int type, char *site, char *file, char *message)
{
char *fileAndMessage;
DWORD size;
char szBuffer[1024];
memset(&szBuffer, NULL, sizeof(szBuffer));
HINTERNET hSession = InternetOpen("App", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET hConnect = InternetConnect(hSession, site, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect != NULL)
{
if (type == RequestType::GET)
sprintf(fileAndMessage, "%s?%s", file, message);
HINTERNET hRequest = HttpOpenRequest(hConnect, (type == RequestType::POST ? "POST" : "GET"), (type == RequestType::POST ? file : fileAndMessage), NULL, NULL, 0, 0, 1);
if (hRequest != NULL)
{
LPCSTR header;
header = "Accept: */*";
HttpAddRequestHeaders(hRequest, header, strlen(header), HTTP_ADDREQ_FLAG_ADD);
header = "Content-Type: application/x-www-form-urlencoded";
HttpAddRequestHeaders(hRequest, header, strlen(header), HTTP_ADDREQ_FLAG_ADD);
if (HttpSendRequest(hRequest, NULL, 0, (type == RequestType::POST ? message : NULL), (type == RequestType::POST ? strlen(message) : NULL)))
{
if (InternetReadFile(hRequest, szBuffer, sizeof(szBuffer), &size))
return szBuffer;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnect);
}
InternetCloseHandle(hSession);
return FALSE;
}