#include <Wininet.h> #pragma comment(lib, "Wininet.lib") BOOL HttpRequestPut(LPCTSTR pHomeUrl, LPCTSTR pPageUrl, LONG nPort, LPCTSTR pFile, CString *psRes, PBOOL pbExit) { LPINTERNET_BUFFERS pBufferIn = new INTERNET_BUFFERS; ZeroMemory(pBufferIn, sizeof(INTERNET_BUFFERS)); LPBYTE pBuf = new BYTE[1024]; HINTERNET hInternet = NULL; HINTERNET hSession = NULL; HINTERNET hRequest = NULL; DWORD dwBytesRead; DWORD dwBytesWritten; HANDLE hFile = INVALID_HANDLE_VALUE; CString sFormat(_T("Connection: Keep-Alive\r\n")); sFormat += _T("Content-Type: application/octet-stream\r\n"); sFormat += _T("Content-Length: %u\r\n"); sFormat += _T("User-Agent:Test\r\n"); sFormat += _T("Host: %s:%u\r\n"); sFormat += _T("Accept: *.*,*/*\r\n"); sFormat += _T("\r\n"); CString sHeader(_T("")); CString sRes(_T("")); do { hInternet = InternetOpen(_T("Mozilla/4.0 (compatible; Indy Library)"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if( NULL == hInternet ) { sRes.Format(_T("Open link error. ErrCode=[%u]"), GetLastError()); break; } hSession = InternetConnect(hInternet, pHomeUrl, (INTERNET_PORT)nPort, NULL, NULL,INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0); hRequest = HttpOpenRequest(hSession, _T("PUT"), pPageUrl, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); if( FALSE == hRequest ) { sRes.Format(_T("Open request handle error. ErrCode=[%u]"), GetLastError()); break; } hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if( hFile == INVALID_HANDLE_VALUE ) { sRes.Format(_T("Open File error. ErrCode=[%u] File=[%s]"), GetLastError(), pFile); break; } pBufferIn->dwStructSize = sizeof(INTERNET_BUFFERS); pBufferIn->dwBufferTotal = GetFileSize(hFile, NULL); sHeader.Format(sFormat, pBufferIn->dwBufferTotal, pHomeUrl, nPort); pBufferIn->lpcszHeader = sHeader; pBufferIn->dwHeadersLength = sHeader.GetLength(); if( FALSE == HttpSendRequestEx(hRequest, pBufferIn, NULL, HSR_INITIATE, 0) ) { sRes.Format(_T("Send request error.")); break; } DWORD dwSendSize = 0; while( dwSendSize < pBufferIn->dwBufferTotal ) { if( (NULL!=pbExit) && (FALSE!=(*pbExit)) ) { sRes.Format(_T("Stop upload because receive exit cmd.")); break; } if( FALSE == ReadFile( hFile, pBuf, 1024, &dwBytesRead, NULL) ) { sRes.Format(_T("Read File error. ErrCode=[%u] File=[%s]"), GetLastError(), pFile); break; } if( FALSE == InternetWriteFile(hRequest, pBuf, dwBytesRead, &dwBytesWritten) ) {// ERROR_INTERNET_CONNECTION_ABORTED sRes.Format(_T("Upload File error. ErrCode=[%u] File=[%s]"), GetLastError(), pFile); break; } dwSendSize += dwBytesWritten; } if( FALSE == HttpEndRequest(hRequest, NULL, 0, 0) ) { sRes.Format(_T("End request error. ErrCode=[%u] File=[%s]"), GetLastError(), pFile); } }while(FALSE); if( hFile != INVALID_HANDLE_VALUE ) { CloseHandle(hFile); } if( hRequest ) { InternetCloseHandle(hRequest); } if( hSession ) { InternetCloseHandle(hSession); } if( hInternet ) { InternetCloseHandle(hInternet); } delete []pBuf; delete pBufferIn; if( NULL != psRes ) { *psRes = sRes; } return sRes.IsEmpty(); }
本文出自:https://blog.csdn.net/mrxyz098/article/details/27991335