http协议 文件上传 POST

来源:互联网 发布:win10网络设置关闭 编辑:程序博客网 时间:2024/05/29 11:18
  1. //封装协议头  
  2. CString CQueenMainDlg::MakeRequestHeaders(CString &strBoundary)// strBoundary 为协议中的boundary  
  3. {  
  4.  CString strFormat=_T("");  
  5.  CString strData =_T("");  
  6.  strFormat += "User-Agent: Mozilla/4.0\r\n";   
  7.  strFormat += "Connection: Keep-Alive\r\n";   
  8.  strFormat += "Accept:*/*\r\n";  
  9.  strFormat += "Accept-Language: zh-cn\r\n";  
  10.  strFormat += _T("Accept-Encoding:gzip,deflate\r\n");  
  11.  strFormat += _T("Content-Type: multipart/form-data; boundary=%s\r\n");  
  12.  strFormat +=_T("Host: %s:%d\r\n");  
  13.  strData.Format(strFormat, strBoundary,m_strSeverName, m_nPort);  
  14.   
  15.    
  16.  return strData;  
  17. }  


 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //封装数据前面的描述部分  
  2. CString  CQueenMainDlg::MakePreFileData(CString &strBoundary, CString &strFileName)  
  3. {  
  4.  //Content-Type:  
  5.  //JPG image/pjpeg  
  6.  //PNG image/x-png  
  7.  //BMP image/bmp  
  8.  //TIF image/tiff  
  9.  //GIF image/gif  
  10.  CString strFormat=_T("");  
  11.  CString strData=_T("");  
  12.  strFormat += _T("--%s");  
  13.  strFormat += _T("\r\n");  
  14.   
  15.  strFormat += _T("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"");  
  16.  strFormat += _T("\r\n");  
  17.  strFormat += _T("Content-Type:image/bmp");//  
  18.  strFormat += _T("\r\n");  
  19.  strFormat += _T("Content-Transfer-Encoding: binary");  
  20.  strFormat += _T("\r\n\r\n");  
  21.  strData.Format(strFormat, strBoundary, strFileName);  
  22.  return strData;  
  23. }  


 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //封装协议尾  
  2. CString  CQueenMainDlg::MakePostFileData(CString &strBoundary)  
  3. {  
  4.  CString strFormat;  
  5.  CString strData;  
  6.  strFormat = _T("\r\n");  
  7.  strFormat += _T("--%s");  
  8.  strFormat += _T("\r\n");  
  9.  strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");  
  10.  strFormat += _T("\r\n\r\n");  
  11.  strFormat += _T("submit");  
  12.  strFormat += _T("\r\n");  
  13.  strFormat += _T("--%s--");  
  14.  strFormat += _T("\r\n");  
  15.  strData.Format(strFormat, strBoundary, strBoundary);  
  16.    
  17.  return strData;  
  18. }  


 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. BOOL  CQueenMainDlg::SendTrack()  
  2. {  
  3.    
  4.  CString m_Url = "http://192.168.1.66:8080/queen_web/imgupload/imgupload.do";// "http://192.168.1.13:65001/aaaa";//  
  5.  LoadSvrAddress(m_Url);  
  6.  CString m_strFilePath = "E:\\lobby1\\queen\\lobby\\";//theApp.m_strAppPath;  
  7.  m_strFilePath += _T("ScreenShot.bmp");  
  8.   
  9.  CString strFileName = _T("ScreenShot.bmp");  
  10.   
  11.  UpdateData(TRUE);  
  12.  CString defServerName =m_strSeverName;  
  13.  CString defObjectName =m_strObject;  
  14.  // USES_CONVERSION;  
  15.  CInternetSession Session;  
  16.  CHttpConnection *pHttpConnection = NULL;  
  17.  INTERNET_PORT   nPort = m_nPort;  
  18.  CFile fTrack;  
  19.  CHttpFile* pHTTP;  
  20.  CString strRequestHeader=_T("");  
  21.  CString strHTTPBoundary=_T("");  
  22.  CString strPreFileData=_T("");  
  23.  CString strPostFileData=_T("");  
  24.  CString strResponse =_T("");  
  25.  DWORD dwTotalRequestLength;  
  26.  DWORD dwChunkLength;  
  27.  DWORD dwReadLength;  
  28.  DWORD dwResponseLength;  
  29.  TCHAR szError[MAX_PATH];  
  30.  void* pBuffer =NULL;  
  31.  LPSTR szResponse;  
  32.   
  33.  BOOL bSuccess = TRUE;  
  34.   
  35.  CString strDebugMessage =_T("");  
  36.   
  37.  if (FALSE == fTrack.Open(m_strFilePath, CFile::modeRead | CFile::shareDenyWrite))  
  38.  {  
  39.   AfxMessageBox(_T("Unable to open the file."));  
  40.   return FALSE;  
  41.  }   
  42.   
  43.  strHTTPBoundary = _T("-----------------------------7d86d16250370");  
  44.  strRequestHeader =MakeRequestHeaders(strHTTPBoundary);  
  45.  strPreFileData = MakePreFileData(strHTTPBoundary, strFileName);  
  46.  strPostFileData = MakePostFileData(strHTTPBoundary);  
  47.   
  48.  MessageBox(strRequestHeader,"RequestHeader",MB_OK | MB_ICONINFORMATION);  
  49.  MessageBox(strPreFileData,"PreFileData",MB_OK | MB_ICONINFORMATION);  
  50.  MessageBox(strPostFileData,"PostFileData",MB_OK | MB_ICONINFORMATION);  
  51.   
  52.  dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength()+ fTrack.GetLength();  
  53.   
  54.  dwChunkLength = 64 * 1024;  
  55.   
  56.  pBuffer = malloc(dwChunkLength);  
  57.   
  58.  if (NULL == pBuffer)  
  59.  {  
  60.   return FALSE;  
  61.  }  
  62.   
  63.  try  
  64.  {  
  65.   pHttpConnection = Session.GetHttpConnection(defServerName,nPort);  
  66.   if(pHttpConnection == NULL)  
  67.   {  
  68.    throw 0 ; //连接服务器失败!  
  69.   }  
  70.   pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);  
  71.   pHTTP->AddRequestHeaders(strRequestHeader);  
  72.   pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);  
  73.   
  74.    
  75.   
  76. #ifdef _UNICODE  
  77.   pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());  
  78. #else  
  79.   pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());  
  80. #endif  
  81.   
  82.   dwReadLength = -1;  
  83.   int count = 1;  
  84.     
  85.   while (0 != dwReadLength)  
  86.   {  
  87.    strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());  
  88.    TRACE(strDebugMessage);  
  89.    dwReadLength = fTrack.Read(pBuffer, dwChunkLength);  
  90.    CString m_str;  
  91.    m_str.Format("count = %d,dwReadLength = %d\n",count,dwReadLength);  
  92.    count++;  
  93.    OutputDebugString(m_str);  
  94.    if (0 != dwReadLength)  
  95.    {  
  96.     pHTTP->Write(pBuffer, dwReadLength);  
  97.    }  
  98.   }  
  99.   
  100. #ifdef _UNICODE  
  101.   pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());  
  102. #else  
  103.   pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());  
  104. #endif  
  105.   
  106.   pHTTP->EndRequest(HSR_SYNC);  
  107.   
  108.   dwResponseLength = pHTTP->GetLength();  
  109.   while (0 != dwResponseLength)  
  110.   {  
  111.    szResponse = (LPSTR)malloc(dwResponseLength + 1);  
  112.    szResponse[dwResponseLength] = '/0';  
  113.    pHTTP->Read(szResponse, dwResponseLength);  
  114.    strResponse += szResponse;  
  115.    free(szResponse);  
  116.    dwResponseLength = pHTTP->GetLength();  
  117.   }  
  118.   //MessageBox(strResponse,"Response",MB_OK | MB_ICONINFORMATION);   
  119.  }  
  120.  catch (CException* e)  
  121.  {  
  122.   e->GetErrorMessage(szError, MAX_PATH);  
  123.   e->Delete();  
  124.   AfxMessageBox(szError);  
  125.   bSuccess = FALSE;  
  126.  }  
  127.  pHTTP->Close();  
  128.  delete pHTTP;  
  129.   
  130.  fTrack.Close();  
  131.   
  132.  if (NULL != pBuffer)  
  133.  {  
  134.   free(pBuffer);  
  135.  }  
  136.  return bSuccess;  

0 0
原创粉丝点击