HTTP POST上传文件代码实现

void UploadFile(LPTSTR lpFilePath)
{
    HINTERNET hSession=0;
    HINTERNET hConnect=0;
    HINTERNET hRequest=0;
    
    DWORD dwNumberOfBytesWritten=0;
    DWORD dwBytesSend=0;


    
    INTERNET_BUFFERS BufferIn;


    DWORD dwFlag;


    LPCTSTR boundary=TEXT("-----------------------------67491722032265"); //随机字符串
    LPCSTR aboundary="-----------------------------67491722032265"; //ansi


    HANDLE hFile;
    hFile=CreateFile(lpFilePath,
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);


    DWORD dwFileSize=GetFileSize(hFile,0);


    
    TCHAR content_type[128];
    _stprintf_s(content_type,TEXT("Content-Type: multipart/form-data; boundary=%s"),boundary);
    LPTSTR referer=TEXT("Referer: http://127.0.0.1/upload/~upload");
    LPTSTR accept=TEXT("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    LPTSTR accept_lan=TEXT("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
    LPTSTR accept_encoding=TEXT("Accept-Encoding: gzip, deflate");
    LPTSTR user_agent=TEXT("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");




    hSession=InternetOpen(_T("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"),
        INTERNET_OPEN_TYPE_PRECONFIG,
        0,
        INTERNET_INVALID_PORT_NUMBER,
        0);
    if (0==hSession)
    {


        return;
    }
        
    hConnect=InternetConnect(hSession,
        TEXT("172.16.1.142"),
        80,
        _T(""),
        _T(""),
        INTERNET_SERVICE_HTTP,
        0,
        0);
    if (0==hConnect)
    {


        InternetCloseHandle(hSession);
        return;
    }






    dwFlag=INTERNET_FLAG_KEEP_CONNECTION;


    hRequest=HttpOpenRequest(hConnect,
        _T("POST"),
        _T("/upload/"),
        HTTP_VERSION,
        0,                //Referrer
        0,                //AcceptTypes 
        dwFlag,
        0);
    if (0==hRequest)
    {


        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }


    HttpAddRequestHeaders(hRequest,content_type,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,referer,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept_lan,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept_encoding,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);


    BYTE* lpBuffer=(BYTE*)VirtualAlloc(0,dwFileSize,MEM_COMMIT,PAGE_READWRITE);
    if (0==lpBuffer)
    {
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }


    DWORD dwRead;
    ReadFile(hFile,lpBuffer,dwFileSize,&dwRead,0);


    CloseHandle(hFile);


    char first_boundary[128];
    char delimiter[128];
    char end_boundary[128];
    sprintf_s(first_boundary,"--%s\r\n",aboundary);
    sprintf_s(delimiter,"\r\n--%s\r\n",aboundary);
    sprintf_s(end_boundary,"\r\n--%s--\r\n",aboundary);


    LPSTR content_dispos="Content-Disposition: form-data; name=\"fileupload1\"; filename=\"notepad.exe\"\r\n";
    
    LPSTR content_type2="Content-Type: application/octet-stream\r\n\r\n";




    LPSTR rn="\r\n";


    BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
    BufferIn.Next = NULL; 
    BufferIn.lpcszHeader = NULL;
    BufferIn.dwHeadersLength = 0;
    BufferIn.dwHeadersTotal = 0;
    BufferIn.lpvBuffer = NULL;                
    BufferIn.dwBufferLength = 0;
    BufferIn.dwBufferTotal = dwFileSize
        +strlen(first_boundary)
        +strlen(content_dispos)
        +strlen(content_type2)
        +strlen(end_boundary); //Content-Length:
    BufferIn.dwOffsetLow = 0;
    BufferIn.dwOffsetHigh = 0;


    if (!HttpSendRequestEx(hRequest,&BufferIn,0,0,0))
    {
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }


    InternetWriteFile(hRequest,(byte*)first_boundary,strlen(first_boundary),&dwNumberOfBytesWritten); //first boundary
    InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
    InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
    InternetWriteFile(hRequest,lpBuffer,dwFileSize,&dwNumberOfBytesWritten);


    //如果还有其他文件
    //InternetWriteFile(hRequest,(byte*)delimiter,strlen(delimiter),&dwNumberOfBytesWritten); //deimiter
    //InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
    //InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
    //...


    InternetWriteFile(hRequest,(byte*)end_boundary,strlen(end_boundary),&dwNumberOfBytesWritten);//last boundary


    HttpEndRequest(hRequest,0,0,0);




    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hSession);


    VirtualFree(lpBuffer,0,MEM_RELEASE);
}

你可能感兴趣的:(HTTP POST上传文件代码实现)