MFC通过Http请求上传文件

FMC通过Http请求上传文件

void CMFCApplication1Dlg::HttpPostFile(string url, CString file, string paramName, string contentType)
{

    CInternetSession pSession(_T("ic_PostWav"));  //可以随意
    CHttpConnection* pConnect;
    CHttpFile *       pFile;

    //通过  url解析出来 
    CString pServeIP = _T("127.0.0.1");
    INTERNET_PORT wPort = 8888;
    CString pObject = _T("/api/v1/documents/upload");

    pConnect = pSession.GetHttpConnection(pServeIP, wPort);
    pFile = pConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, pObject, NULL, 0, NULL, NULL, INTERNET_FLAG_DONT_CACHE);

    string boundary = "----1a2b3c4d5e6f";
    //Http 头部  
    string pPostHeader;
    pPostHeader = "Accept:audio/x-wav,text/html,application/xhtml+xml,application/xml,*/*;q=0.9,*/*;q=0.8\r\n";
    pPostHeader += "Content-Type: multipart/form-data;";
    pPostHeader += "boundary=" + boundary + "\r\n";
    pPostHeader += "Connection: keep-alive\r\n";
    CString  httpHead;
    httpHead.Format(_T("%s"), pPostHeader);
    pFile->AddRequestHeaders(httpHead);

    //数据帧头  
    string dataTop, name,filename;
    name = "name";
    filename = "filename";

    dataTop = "--"+ boundary +"\r\n";
    dataTop += "Content-Disposition:form-data;";
    dataTop += "name=\"" + name + "\";";
    dataTop += "filename=\"" + filename + "\"\r\n";
    dataTop += "Content-Type:" + contentType + "\r\n\r\n";

    //byte* pPostTopbytes = new byte[dataTop.length()];  //todo
    //for (int i = 0; i < dataTop.length();i++)
    //{
    //  pPostTopbytes[i] = (byte)dataTop[i];
    //}

    byte* pPostTopbytes = (byte*)dataTop.c_str();

    //数据包尾  
    string dataEnd;
    dataEnd = "\r\n--" + boundary + "--\r\n";
    //byte* enderbyte = new byte[ dataEnd.size()]; //todo
    //for (int i = 0; i < dataEnd.length(); i++)
    //{
    //  enderbyte[i] = (byte)dataEnd[i];
    //}

    byte* enderbyte = (byte*)dataEnd.c_str();


    CFile cfile;
    cfile.Open(file, CFile::modeRead | CFile::shareDenyRead, NULL);

    DWORD dwSize = dataTop.length() + dataEnd.length() + cfile.GetLength();
    pFile->SendRequestEx(dwSize);

    //写数据头
    pFile->Write(pPostTopbytes, dataTop.length());

    //写数据主体  
    int bufflength = 4 * 1024;
    byte* buffer = new byte[bufflength];
    int byteRead = 0;
    while ((byteRead = cfile.Read(buffer, bufflength)) != 0)
    {
        pFile->Write(buffer, byteRead);
    }
    cfile.Close();

    //写数据尾部
    pFile->Write(enderbyte, dataEnd.length() );
    //发送文件
    pFile->EndRequest();

    //接收返回
    CString strSentence = _T(""), strGetSentence = _T("");
    DWORD dwRet;
    pFile->QueryInfoStatusCode(dwRet);
    if (HTTP_STATUS_OK == dwRet)
    {
        while (pFile->ReadString(strSentence))   // 读取提交数据后的返回结果  
        {
            strGetSentence = strGetSentence + strSentence;
        }
        char * temp = (char*)strGetSentence.GetBuffer(strGetSentence.GetLength());
        string reponce(temp);  
        //todo:  将返回的编码数据转为自己需要的编码数据
    }
    pFile->Close();
    pConnect->Close();
}

void CMFCApplication1Dlg::OnBnClickedOk()
{
    //调用方式
    HttpPostFile("", L"E:\\123\\内墙With柱fbx.pcx"/*L"E:\\123\\test.txt"*/, "uploadeFile", "application/octet-stream" /*L"text/plain"*/);
}

你可能感兴趣的:(网络通信)