使用Wininet将文件上传到http表单,使用post方法(CGI)

时间:2012-02-14 10:29:29

标签: c++ http mfc wininet

BOOL CInternet::UpgradeFirmware(char* sIPAddress, int nPort, char* sRequest, char* sResponse,` char* sUserName, char* sPassword,CString sFileName)
{
    BOOL bReturn = FALSE;
    DWORD dwRet = 0;
    strcpy(sResponse, "");

    CPing ping;
    CPingReply pr;
    if ( ping.Ping(sIPAddress,pr) != SUCCEEDED/*FALSE*/)        //  ping once only
    {
        //can not ping this address, return FALSE
        return FALSE;
    }

    CInternetSession session;
    CHttpConnection* pConnection = NULL;   
    CHttpFile* pFile = NULL;
    CFile file;
    DWORD dwTotalRequestLength;
    int cReadLength;
    DWORD dwChunkLength = 64*1024; 
    void* pBuffer = malloc(dwChunkLength);

    session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 600000); 
    session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1); 
    session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 600000);
    session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 60000);
    try
    {   
        file.Open(sFileName,CFile::modeRead);
        CString strHTTPBoundary = _T("AsCg5y");       
        CString strPreFileData = MakePreFileData(strHTTPBoundary, file.GetFileName(),file.GetLength());
        CString strPostFileData = MakePostFileData(strHTTPBoundary);
        CString strRequestHeaders = MakeRequestHeaders(strHTTPBoundary);
        dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + file.GetLength(); 
        pConnection = session.GetHttpConnection(sIPAddress,0, nPort, sUserName, sPassword); 
        DWORD dwHttpRequestFlags =  
                    INTERNET_FLAG_KEEP_CONNECTION
                    | INTERNET_FLAG_NO_UI
                    | INTERNET_FLAG_NO_COOKIES
                    | INTERNET_FLAG_RESYNCHRONIZE 
                    | INTERNET_FLAG_RELOAD
                    |INTERNET_FLAG_NEED_FILE;

        //////////////////////////////////////////////////////////////////////////////
        ///////CHttpConnection::HTTP_VERB_POST for Upload Method/////////////////////
        pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, sRequest, "HTTP/1.0", 1, NULL, NULL,dwHttpRequestFlags);
        pFile->AddRequestHeaders(strRequestHeaders); 
        pFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE );  
        pFile->Write((LPSTR)(LPCTSTR)strPreFileData, strPreFileData.GetLength()); 

        cReadLength = -1; 
        while (cReadLength!=0)
        {
            cReadLength = file.Read(pBuffer, dwChunkLength);
            if (0 != cReadLength)
            {
                pFile->Write(pBuffer,cReadLength);
            }
        }  
        file.Close();
        pFile->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
        pFile->EndRequest(HSR_SYNC);
        //////////////////////////////////////////////////////////////////////// 
        pFile->QueryInfoStatusCode(dwRet); 

        if(dwRet == HTTP_STATUS_OK)
        {
            bReturn = TRUE;
        } 
        else
        {
            bReturn = FALSE; 
        }
    }
    catch(CInternetException* pEx)
    {   
        pEx->Delete();
        pEx = NULL;
        bReturn = TRUE;
    } 
    if(pFile)
    {
        pFile->Close();
        delete pFile;
    }

    if(pConnection)
    {
        pConnection->Close();
        delete pConnection;
    }

    session.Close(); 
    return bReturn;
}

pFile-> Write(pBuffer,cReadLength); 会抛出异常并显示以下消息:

  

ERROR_INTERNET_CONNECTION_ABORTED   12030   与服务器的连接已终止。

我不明白原因。请帮帮我!

0 个答案:

没有答案