Login dark

`
title: MFC使用POST上传文件
author: Love02xp
date: 2019-12-12 01:42:58
category: [编程代码]
tags: [代码,VC6]



include <afxinet.h>,关键是要注意http的post协议格式,以及编码(Unicode下需要进行转换)。主要分为以下几个步骤:


<!--more-->

1、 编写数据包的头:

  这里需要注意一个是”Accept: audio/x-wav,text/plain,*/*;….”表示的是希望服务器接收的数据格式;还有就是”Content-Type: multipart/form-data;boundary=\%s”,表示是上传文件,boundary只是划分的分隔符(一般以”``````````````````7b1ac890a1b”的形式存在);
2、 编写传输的主数据体:

可以理解为将不同的数据通过boundary分割开来,每类数据分别发送。首行的boundary前面需要多加”--”。格式为:

Content-Disposition:form-data; name=”name”; filename=”filename”

Content-Type: audio/x-wav

....(wav的byte流数据)
3、 编写数据包尾:

数据包结尾最后一个boundary后要多加”--”。格式为:


Content-Disposition:form-data; name=”Submit”

 

Submit

下面是代码:

void CCPostDlg::HttpPostFile( CString url, CString file, CString paramName, CString contentType )
 
{
 
     CString boundary = "```````````````````````````1a2b3c4d5e6f";//分隔符,注意:首行必须多"--",尾部最后必须加"--",否则识别不出。
 
     CString boundaryTemplate;
 
     boundaryTemplate.Format(_T("\r\n--%s\r\n") , boundary);
 
     byte* boundarybytes = (byte*)boundaryTemplate.GetBuffer(boundaryTemplate.GetLength());
 
 
 
     CInternetSession pSession("ic_PostWav");
 
     CHttpConnection* pConnect;
 
     CHttpFile*         pFile;
 
 
 
     CString pServeIP = _T("192.168.10.156");
 
     INTERNET_PORT wPort = 80;
 
     CString pObject = _T("/yxbp/index.php");
 
     pConnect= pSession.GetHttpConnection(pServeIP , wPort);
 
     pFile= pConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, pObject
 
         ,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
 
 
 
     //数据包头
 
     CString pPostHeader , pPostHeaderTemplate;
 
     pPostHeaderTemplate= _T("Accept:audio/x-wav,text/html,application/xhtml+xml,application/xml,*/*;q=0.9,*/*;q=0.8\r\n")
 
         _T("Content-Type: multipart/form-data;boundary=\%s\r\n")
 
         _T("Connection: keep-alive\r\n");
 
     pPostHeader.Format(pPostHeaderTemplate, boundary);
 
     pFile->AddRequestHeaders(pPostHeader);
 
 
 
     //数据帧头
 
     CString pPostTop , pPostTopTemplate;
 
     pPostTopTemplate= _T("\%sContent-Disposition:form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: \%s\r\n\r\n");
 
     pPostTop.Format(pPostTopTemplate  , boundaryTemplate,_T("file"), file , contentType);
 
     byte* pPostTopbytes = (byte*)pPostTop.GetBuffer(pPostTop.GetLength());
 
 
 
     //数据包尾
 
     CString ender,enderTemplate;
 
     enderTemplate= _T("\r\n--\%s\r\nContent-Disposition:form-data; name=\"\%s\"\r\n\r\n\%s");
 
     ender.Format(enderTemplate, boundary , _T("Ender") , _T("ender"));
 
     ender+= _T("\r\n--");
 
     ender+= boundary;
 
     ender+= _T("--\r\n");
 
     byte* enderbyte = (byte*)ender.GetBuffer(ender.GetLength());
 
 
 
     CFile cfile;
 
     cfile.Open(file , CFile::modeRead|CFile::shareDenyRead, NULL);
 
 
 
     DWORD dwSize = pPostTop.GetLength() + ender.GetLength() + cfile.GetLength();
 
     pFile->SendRequestEx(dwSize);
 
     pFile->Write(pPostTopbytes, pPostTop.GetLength());
 
 
 
     //数据主体
 
     int bufflength = 4 * 1024;
 
     byte* buffer = newbyte[bufflength];
 
     int byteRead = 0;
 
     while((byteRead = cfile.Read(buffer , bufflength)) != 0)
 
     {
 
         pFile->Write(buffer , byteRead);
 
     }
 
     cfile.Close();
 
 
 
     //写尾
 
     pFile->Write(enderbyte, ender.GetLength());
 
     pFile->EndRequest();
 
 
 
     CString strSentence = "",strGetSentence = "";
 
     DWORD dwRet ;
 
     pFile->QueryInfoStatusCode(dwRet);
 
     if(HTTP_STATUS_OK==dwRet)
 
     {
 
         while(pFile->ReadString(strSentence))   // 读取提交数据后的返回结果
 
         {
 
              strGetSentence = strGetSentence+ strSentence ;
 
          }
 
     }
 
    
 
     pFile->Close();
 
     pConnect->Close();
 
}

调用:

HttpPostFile("http://127.0.0.1/xxxx/index.php","C:\temp.wav" , "file","audio/x-wav");