winnet http上传实现

//一个HTTP上传  wininet实现
//很是奇怪为何直接HttpSendRequest方式会不成功,而用这种InternetWriteFile 可以完成

int UploadFile(char* szPath)
{
    HINTERNET hSession=0,hConnect=0,hRequest=0;
    DWORD dwNumberOfBytesWritten=0;
    DWORD dwBytesSend=0;
    DWORD dwFlag;

    INTERNET_BUFFERS BufferIn;

    HANDLE hFile;
    hFile=CreateFile(szPath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    DWORD dwFileSize=GetFileSize(hFile,0);

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

    hSession=InternetOpen("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 -1;
    }
        
    hConnect=InternetConnect(hSession,192.168.1.1,8080,"","",INTERNET_SERVICE_HTTP,0,0);
    if (0==hConnect)
    {
        InternetCloseHandle(hSession);
        return -2;
    }

    dwFlag=INTERNET_FLAG_KEEP_CONNECTION;
    hRequest=HttpOpenRequest(hConnect,"POST","",HTTP_VERSION,0,0,dwFlag,0);
    if (0==hRequest)
    {
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return -3;
    }

    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 -4;
    }

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

    CloseHandle(hFile);

    char first_boundary[128];
    char delimiter[128];
    char end_boundary[128];
    char content_dispos[128];
    char content_type2[128];
    sprintf_s(first_boundary,"--%s\r\n",boundary);
    sprintf_s(delimiter,"\r\n--%s\r\n",boundary);
    sprintf_s(end_boundary,"\r\n--%s--\r\n",boundary);
    sprintf_s(content_dispos,"Content-Disposition: form-data; name=\"fileupload\";filename=\"filename\"\r\n");
    sprintf_s(content_type2,"Content-Type: application/octet-stream\r\n\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 -5;
    }

    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);
    return 1;
}



你可能感兴趣的:(C++)