VC6.0 OCX模拟http/post上传文本文件

来源:互联网 发布:数据库日常维护 编辑:程序博客网 时间:2024/06/10 15:46

感谢这者两位作者,我将他们两个的代码整合了一下,本来以为用第一个链接里面的代码拷贝过来修改就可以用了,可是却一直提示服务器无法访问,第二个链接里面的代码虽然可以连接到服务器,但是却无法上传文件,所以将两个代码整合了一下,如果有纰漏,希望大家给出建议:

http://blog.csdn.net/angellove156/article/details/41045583

http://www.docin.com/p-660772334.html

BSTR CUSBDataCollectionCommCtrl::MakePreFileData(LPCTSTR strBoundary, LPCTSTR strFileName) 
{
CString strFormat;  
    CString strData;  
  
    strFormat += _T("--%s");  
    strFormat += _T("\r\n");  
    strFormat += _T("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"");  
  
    strFormat += _T("\r\n");  
    strFormat += _T("Content-Type: text/plain");  
  
    strFormat += _T("\r\n\r\n");  
    strData.Format(strFormat,  strBoundary, strFileName); 
   
return strData.AllocSysString();
}


BSTR CUSBDataCollectionCommCtrl::MakePostFileData(LPCTSTR strBoundary) 
{
// TODO: Add your dispatch handler code here
CString strFormat;  
    CString strData;  
    strFormat = _T("\r\n");   
    strFormat += _T("--%s");  
    strFormat += _T("\r\n");  
    strFormat += _T("Content-Disposition: form-data; name=\"upload\"");  
    strFormat += _T("\r\n\r\n");  
    strFormat += _T("hello");  
    strFormat += _T("\r\n");  
    strFormat += _T("--%s--");  
    strFormat += _T("\r\n");  
    strData.Format(strFormat, strBoundary, strBoundary);  
    return strData.AllocSysString();  
}


void CUSBDataCollectionCommCtrl::UploadFile(LPCTSTR strFileName, LPCTSTR strIP, LPCTSTR nPort, LPCTSTR strRemote) //strIP填写完整的地址,例如http://127.0.0.1:8080/xxxxxxxx后两个参数在这里没什么用,因为VC6.0的ACTIVEX工程用的不熟,我只会删了重新定义。。。所以这样贴上来的,有会的朋友求简单方法~~
{
CInternetSession Session;  
    CHttpConnection *pHttpConnection = NULL;  
    CFile fTrack;  
    CHttpFile* pHTTP = NULL;  
    CString strHTTPBoundary;  
    CString strPreFileData;  
    CString strPostFileData;  
    DWORD dwTotalRequestLength = 0;  
    DWORD dwChunkLength = 0;  
    DWORD dwReadLength = 0;  
    DWORD dwResponseLength = 0;  
    TCHAR szError[MAX_PATH] = {0};  
    void* pBuffer = NULL;  
    LPSTR szResponse;  
    CString strResponse;  
    BOOL bSuccess = TRUE;  
    CString strDebugMessage; 
BOOL bResult = FALSE;
DWORD dwType = 0;
CString strServer;
CString strObject;
INTERNET_PORT wPort = 0;
bResult = AfxParseURL(strIP,dwType,strServer,strObject,wPort);//加了这句就可以连接服务器了
if(!bResult)
return;
    //读取文件  
    if (FALSE == fTrack.Open(strFileName, CFile::modeRead | CFile::shareDenyWrite))  
    {  
        FireSendMessage(_T("Open File Failed path") + CString(strFileName));  
        return;  
    }  
    int iRecordID = 1;  
    strHTTPBoundary = _T("---------------------------7b4a6d158c9");//定义边界值  
    CString pcmname = strFileName;  
    pcmname = pcmname.Mid(pcmname.ReverseFind('\\') + 1);//获取要上传的文件的名字
  
    FireSendMessage("获取的文件名: " + pcmname);  
  
    strPreFileData = MakePreFileData(strHTTPBoundary, pcmname);  
    strPostFileData = MakePostFileData(strHTTPBoundary);  
    dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();//计算整个包的总长度  
  
  
    dwChunkLength = fTrack.GetLength();  
    pBuffer = malloc(dwChunkLength);  
    if (NULL == pBuffer)  
    {  
        FireSendMessage("Fun_UploadPic 申请内存失败 长度 = " + dwChunkLength);  
        fTrack.Close();  
        return;  
    }  
  
    try  
    {  
        pHttpConnection = Session.GetHttpConnection(strServer,wPort);
//FireSendMessage("pHttpConnection 0K!");
        pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);  
        if(NULL==pHTTP)  
            return;  
FireSendMessage("pHttpConnection 0K!");
        pHTTP->AddRequestHeaders((CString)MakeRequestHeaders(strHTTPBoundary));//发送包头请求  
  
        pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);  
  
#ifdef _UNICODE  
        pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());  
#else  
        pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());  
#endif  
  
        dwReadLength = -1;  
        while (0 != dwReadLength)  
        {  
                strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());  
  
            TRACE(strDebugMessage);  
            dwReadLength = fTrack.Read(pBuffer, dwChunkLength);  
            if (0 != dwReadLength)  
            {  
                pHTTP->Write(pBuffer, dwReadLength);//写入服务器本地文件,用二进制进行传送  
            }  
        }  
  
#ifdef _UNICODE  
        pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());  
#else  
        pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());  
#endif  
  
            pHTTP->EndRequest(HSR_SYNC);  
        dwResponseLength = pHTTP->GetLength();  
        while (0 != dwResponseLength)  
        {  
                szResponse = (LPSTR)malloc(dwResponseLength + 1);  
            szResponse[dwResponseLength] = '\0';  
            pHTTP->Read(szResponse, dwResponseLength);  
            strResponse += szResponse;  
            free(szResponse);  
            dwResponseLength = pHTTP->GetLength();  
        }  
    }  
    catch (CException* e)  
    {  
        e->GetErrorMessage(szError, MAX_PATH);  
        e->Delete();  
        AfxMessageBox(szError);  
        bSuccess = FALSE;  
    }  
  
    pHTTP->Close();  
    delete pHTTP;  
  
    fTrack.Close();  
  
    if (NULL != pBuffer)  
    {  
        free(pBuffer);  
    }  
}


BSTR CUSBDataCollectionCommCtrl::MakeRequestHeaders(LPCTSTR strBoundary) 
{
CString strFormat;  
    CString strData;  
    strFormat = _T("Content-Type: multipart/form-data; boundary=%s\r\n");  
    strData.Format(strFormat, strBoundary);  
    return strData.AllocSysString();  
}

0 0
原创粉丝点击