1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <stdio.h> #include <Windows.h> #include <winhttp.h> #pragma comment (lib, "winhttp.lib") int main() { DWORD dwSize = 0, dwDownloaded; LPSTR source; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; BOOL bResults = FALSE; hSession = WinHttpOpen(L"Winhttp API", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"255.255.255.255", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/set_data.php?coordinate_x=123&coordinate_y=11", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); if (hRequest) bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL); if (bResults) { do { dwSize = 0; if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); source = (char *)malloc(dwSize + 1); if (!source) { printf("Out of memory\n"); dwSize = 0; } else { ZeroMemory(source, dwSize + 1); if (!WinHttpReadData(hRequest, (LPVOID)source, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); else printf("%s\n", source); free(source); } }while (dwSize > 0); } if (!bResults) printf("Error %d has occurred.\n", GetLastError()); if (hRequest) WinHttpCloseHandle(hRequest); if (hConnect) WinHttpCloseHandle(hConnect); if (hSession) WinHttpCloseHandle(hSession); return 0; } |
위 소스에서 WinHttpOpenRequest 부분에 있는 L”/set_data.php?coordinate_x=123&coordinate_y=11″ 부분을 변수로 바꾸려면 아래와 같이 하면 됩니다.
1 2 3 4 5 6 7 |
wchar_t str[256]; wsprintf(str, L"/set_data.php?coordinate_x=%d&coordinate_y=%d", x, y); LPCWSTR name = str; .... hRequest = WinHttpOpenRequest(hConnect, L"GET", name, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); |
참고 : https://msdn.microsoft.com/en-us/library/aa384270(v=vs.85).aspx
참고자료 : http://scor7910.tistory.com/11