如何通过mFC / C ++中的HTTP post方法发送Zip(二进制)文件?

时间:2010-12-20 05:00:57

标签: c++ mfc

我将文件发布到服务器并且工作正常,但是当我尝试发布.zip文件时,我的代码失败了。可能是我的代码在读取zip文件内容数据时出错了。

ifstream::pos_type size;
char * memblock;
ifstream file ("example.zip", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
 size = file.tellg();
 memblock = new char [size];
 file.seekg (0, ios::beg);
 file.read (memblock, size);
 file.close();

 postBody.AppendFormat("Content-Disposition: form-data; name=\"datafile\"; filename=\"%s\"; \r\n\n%s", zipFilePath, memblock); 
 postBody.AppendFormat("\r\n--%s--\r\n", boundary);
}

1 个答案:

答案 0 :(得分:1)

最后我得到了这个问题的答案:

 CString boundary = "-------------------------------7d8b32060180";
          CString postBody(_T(""));
          postBody.AppendFormat("\r\n--%s\r\n", boundary);

          postBody.AppendFormat("Content-Disposition: form-data; name=\"name\"\n\n%s", name);
          postBody.AppendFormat("\n--%s\r\n", boundary);

          postBody.AppendFormat("Content-Disposition: form-data; name=\"email\"\n\n\n\r\r\n%s\n", emailID);
          postBody.AppendFormat("\r\n--%s\r\n", boundary);

          postBody.AppendFormat("Content-Disposition: form-data; name=\"comments\"\n\n\r\r\n%s\n", comment);
          postBody.AppendFormat("\r\n--%s\r\n", boundary);

          postBody.AppendFormat("Content-Disposition: form-data; name=\"subject\"\n\n\r\r\n%s\n", subjectLine);  
          postBody.AppendFormat("\r\n--%s\r\n", boundary);

          int  httpReqBodyLen    = 0;
          BYTE* httpRequestBody  = NULL;              
          TCHAR* fileBuff        = NULL;
          int fileBufLen         = 0; 
          if (!filePath.IsEmpty())
          {                       
                CFile vidFile; 
                if (vidFile.Open(filePath, CFile::modeRead))             
                {
                      fileBufLen = vidFile.GetLength();
                      fileBuff = new TCHAR[fileBufLen + 1];
                      vidFile.Read(fileBuff, fileBufLen);
                      fileBuff[fileBufLen] = '\0';
                      vidFile.Close();
                      postBody.AppendFormat("Content-Disposition: form-data; name=\"datafile\"; filename=\"%s\"; \r\n\n", filePath);  
                }     
          }

          httpReqBodyLen = postBody.GetLength() + strlen("\n--" + boundary + "--\r\n") + fileBufLen + 1;
          httpRequestBody = new BYTE[httpReqBodyLen];  //create enough buffer memory
          memset(httpRequestBody, 0x00, httpReqBodyLen);
          memcpy(httpRequestBody, postBody.GetString(), postBody.GetLength()); //write header to final buffer
          if (!filePath.IsEmpty() && fileBuff)
          {
                memcpy(httpRequestBody + postBody.GetLength(), fileBuff, fileBufLen); //add file contents
                delete fileBuff;
          }

          memcpy(httpRequestBody + postBody.GetLength() + fileBufLen, _T("\n--" + boundary + "--\r\n"), strlen("\n--" +boundary+ "--\r\n")); 
          httpRequestBody[httpReqBodyLen - 1] = '\0';

          CString strHeaderCS;
          strHeaderCS.AppendFormat("Content-Type:multipart/form-data; boundary=%s\r\n", boundary);
          strHeaderCS.AppendFormat("Accept-Charset:ISO-8859-15, utf-8;q=0.66, *;q=0.66\r\n");
          strHeaderCS.AppendFormat("Keep-Alive:5000\r\n");
          strHeaderCS.AppendFormat("Accept-Language:en\r\n");
          strHeaderCS.AppendFormat("Accept-Encoding:gzip, deflate\r\n");
          strHeaderCS.AppendFormat("User-Agent:Mozilla/4.0 (Windows; U; Windows Vista;)\r\n");
          strHeaderCS.AppendFormat("Accept:text/xml,application/xml,application/xhtml+xml, application/vid,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
          strHeaderCS.AppendFormat("Content-Length:%d\r\n", httpReqBodyLen);

          strHeaderCS.AppendFormat("Connection:keep-alive\r\n");
          strHeaderCS.AppendFormat("Host:www.filemagic.org\r\n");

          CString strObject, server;
          INTERNET_PORT port;
          DWORD dwServiceType;
          USES_CONVERSION;
          AfxParseURL(strURL, dwServiceType, server, strObject, port);
          HINTERNET hINetOpen, hINetConnect, hINetRequest;
          try
          {
                hINetOpen = InternetOpen(_T("SplashID"),  INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);//AK
                if (hINetOpen != NULL){
                      hINetConnect = InternetConnect(hINetOpen, server, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_IDN_PROXY, 0); 
                      if (hINetConnect != NULL){
                            hINetRequest = HttpOpenRequest(hINetConnect, _T("POST"), strObject, HTTP_VERSION, 0, NULL,  INTERNET_FLAG_RELOAD |INTERNET_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_UNKNOWN_CA | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, 0);
                            if (hINetRequest != NULL){
                                  if(HttpSendRequest(hINetRequest, strHeaderCS, strHeaderCS.GetLength(), (LPVOID) httpRequestBody, httpReqBodyLen)) {
                                        CString sBodyText;
                                        DWORD nRead= 1024;
                                        char szBuf[ 1025 ] = {0};
                                        while ( nRead > 0 ) {
                                              // Read in a temporary buffer
                                              InternetReadFile(hINetRequest, szBuf, 1024, &nRead);
                                              szBuf[nRead] = '\0';
                                              sBodyText += szBuf;
                                        }
                                  }                                   
                            }
                      }
                }
          }
          catch(...)
          {
          }
          if (threadData)
          {
              delete threadData;
              threadData = NULL;
          }
          if (httpRequestBody)
          {
              delete httpRequestBody;
              httpRequestBody = NULL;
          }
          if(hINetOpen)
          {
              InternetCloseHandle(hINetOpen);
              hINetOpen = NULL;
          }
          if (hINetConnect)
          {
              InternetCloseHandle(hINetConnect);
              hINetConnect = NULL;
          }
          if (hINetRequest)
          {
              InternetCloseHandle(hINetRequest);
              hINetRequest = NULL;
          }