int httpSendPost(string server, string path, string data) { char* buf = &data[0]; string url = path + data; //combine path + data wstring url1 = wstring(url.begin(), url.end()); // turn url into a wide string wstring server1 = wstring(server.begin(), server.end()); // turn server into wide string GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, server1.c_str(), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("POST"), url1.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0); if (hRequest == NULL) cout << "HttpOpenRequest error code: " << GetLastError() << endl; if (!HttpSendRequest(hRequest, 0, 0, buf, data.length())) cout << "HttpSendRequest error code: " << GetLastError() << endl; InternetCloseHandle(hInternet); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; } int httpSendGet(string server, string path, string data) { string url = path + data; //combine path + data wstring url1 = wstring(url.begin(), url.end()); // turn url into a wide string wstring server1 = wstring(server.begin(), server.end()); // turn server into wide string GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, server1.c_str(), 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; } int toTempFile(string fileName, string toFile) { wstring fileName1 = wstring(fileName.begin(), fileName.end()); wstring path; wchar_t wchPath[MAX_PATH]; if (GetTempPathW(MAX_PATH, wchPath)) path = wchPath; path = path.append(fileName1.c_str()); ofstream out(path, ios::app); out << toFile << endl; out.close(); return 0; }
Friday, April 28, 2017
C++ Helper Functions for Output to File/HTTP GET/HTTP POST
Here's the combination of helper functions I'm using to send data to either files or an HTTP server, variable filename, server, URI path, etc...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment