2010-12-20 61 views
0

我發佈文件到服務器和它的工作正常,但我的代碼失敗時,我嘗試發佈.zip文件。可能是我的代碼在讀取zip文件內容數據時出錯。如何發送Zip(二進制)文件通過mFC/C++中的HTTP post方法?

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); 
} 
+0

請回答這個問題............ – Mahantesh 2010-12-20 06:00:50

回答

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; 
      } 
+0

能否請你解釋「Mahantesh」,它的頭文件,你用於解決這個問題! – 2011-02-08 12:47:22

+0

#include「afxinet.h」是必需的。 – Mahantesh 2011-02-09 07:04:45

+0

最後提交的代碼有一些內存泄漏,因此重新提交了代碼。 – Mahantesh 2011-02-09 07:07:16

相關問題