Wednesday, April 26, 2017

C++ Function For HTTP GET

Also often used function for sending HTTP GET requests...


int httpSendGet(string data)
{
 string url = "/hi/there/" + data;
 wstring url1 = wstring(url.begin(), url.end());
 GetLastError();
 HINTERNET hInternet, hConnect, hRequest;
 hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
 hConnect = InternetConnect(hInternet, TEXT("www.site.com"), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
 hRequest = HttpOpenRequest(hConnect, TEXT("GET"), url1.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0);
 if (hRequest == NULL)
  cout << "HttpOpenRequest error code: " << GetLastError() << endl;
 if (!HttpSendRequest(hRequest, 0, 0, NULL, NULL))
  cout << "HttpSendRequest error code: " << GetLastError() << endl;
 InternetCloseHandle(hInternet);
 InternetCloseHandle(hConnect);
 InternetCloseHandle(hRequest);
 return 0;
}

No comments:

Post a Comment