VC实现Http Put方法

该函数可用于向服务器上传文件。服务器端可以有专门的接收机制,也可以没有。
#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();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值