Skip to content

Instantly share code, notes, and snippets.

@mokapyo
Created April 26, 2017 20:27
Show Gist options
  • Save mokapyo/6c03d905a2a3cbf860caddda9c86ccb2 to your computer and use it in GitHub Desktop.
Save mokapyo/6c03d905a2a3cbf860caddda9c86ccb2 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <winhttp.h>
#include <atlstr.h>
#pragma comment (lib, "winhttp.lib")
int main() {
HINTERNET hSession, hConnect, hRequest;
URL_COMPONENTS urlComponents;
WCHAR szHostName[256], szUrlPath[2048];
WCHAR szUrl[] = L"http://127.0.0.1/test.php"; // 受信側アプリ
CStringA Header = "Content-Type: application/x-www-form-urlencoded\r\n"; //ヘッダ部送信
CString SendFilePath = "C:\\test\\SendFile.txt"; // 送りたいファイル
DWORD dwTotalLength;
DWORD dwSize;
DWORD dwStatusCode;
BYTE buffer[4096];
DWORD SEND_BLOCK_SIZE = 1024 * 1024; // ファイル分割サイズ
// サーバー接続
hSession = WinHttpOpen(L"Sample Application/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession == NULL)
return 0;
ZeroMemory(&urlComponents, sizeof(URL_COMPONENTS));
urlComponents.dwStructSize = sizeof(URL_COMPONENTS);
urlComponents.lpszHostName = szHostName;
urlComponents.dwHostNameLength = sizeof(szHostName) / sizeof(WCHAR);
urlComponents.lpszUrlPath = szUrlPath;
urlComponents.dwUrlPathLength = sizeof(szUrlPath) / sizeof(WCHAR);
if (!WinHttpCrackUrl(szUrl, lstrlenW(szUrl), 0, &urlComponents)) {
WinHttpCloseHandle(hSession);
return 0;
}
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_PORT, 0);
if (hConnect == NULL) {
WinHttpCloseHandle(hSession);
return 0;
}
hRequest = WinHttpOpenRequest(hConnect, L"POST", szUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (hRequest == NULL) {
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 0;
}
// ヘッダ部送信
if (!WinHttpAddRequestHeaders(hRequest, (LPCWSTR)CStringW(Header), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD)) {
return 0;
}
// ボディ部送信
HANDLE hFile = CreateFile((LPCTSTR)SendFilePath, GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, nullptr);
// これだけ送るからよろしく!
WinHttpSendRequest(hRequest, nullptr, WINHTTP_NO_ADDITIONAL_HEADERS, WINHTTP_NO_REQUEST_DATA, 0, dwFileSize, 0);
BYTE* Block = new BYTE[SEND_BLOCK_SIZE];
DWORD dwSentSize = 0;
while (dwSentSize <= dwFileSize) {
DWORD dwGetSize = 0;
DWORD len = (dwSentSize + SEND_BLOCK_SIZE < dwFileSize) ? SEND_BLOCK_SIZE : dwFileSize - dwSentSize;
SetFilePointer(hFile, dwSentSize, NULL, FILE_BEGIN);
ReadFile(hFile, Block, len, &dwGetSize, NULL);
if (!WinHttpWriteData(hRequest, Block, len, &dwGetSize)) { // 指定サイズごとにWiteData
return 0;
}
dwSentSize = dwSentSize + len;
}
delete[] Block;
// おしまい
if (!WinHttpReceiveResponse(hRequest, NULL)) {
return 0;
}
dwSize = sizeof(DWORD);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwStatusCode, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (dwStatusCode == HTTP_STATUS_OK) {
WinHttpReadData(hRequest, buffer, sizeof(buffer), NULL);
MessageBoxA(NULL, (LPSTR)buffer, "ボディ", MB_OK);
} else {
TCHAR szBuf[256];
wsprintf(szBuf, TEXT("Status Code %d"), dwStatusCode);
MessageBox(NULL, szBuf, NULL, MB_ICONWARNING);
}
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment