工作问题积累(七)http协议 文件上传 POST

        前段时间公司希望做个截屏功能,并且能够将图片发送到网站上面去,这个是我的实现的功能代码,这个不能直接运行,需要自己写个类,把这些函数放到类中,再去调用就可以了,功能是没有问题的。

 

//封装协议头
CString CQueenMainDlg::MakeRequestHeaders(CString &strBoundary)// strBoundary 为协议中的boundary
{
 CString strFormat=_T("");
 CString strData =_T("");
 strFormat += "User-Agent: Mozilla/4.0\r\n"; 
 strFormat += "Connection: Keep-Alive\r\n"; 
 strFormat += "Accept:*/*\r\n";
 strFormat += "Accept-Language: zh-cn\r\n";
 strFormat += _T("Accept-Encoding:gzip,deflate\r\n");
 strFormat += _T("Content-Type: multipart/form-data; boundary=%s\r\n");
 strFormat +=_T("Host: %s:%d\r\n");
 strData.Format(strFormat, strBoundary,m_strSeverName, m_nPort);

 
 return strData;
}


 

//封装数据前面的描述部分
CString  CQueenMainDlg::MakePreFileData(CString &strBoundary, CString &strFileName)
{
 //Content-Type:
 //JPG image/pjpeg
 //PNG image/x-png
 //BMP image/bmp
 //TIF image/tiff
 //GIF image/gif
 CString strFormat=_T("");
 CString strData=_T("");
 strFormat += _T("--%s");
 strFormat += _T("\r\n");

 strFormat += _T("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"");
 strFormat += _T("\r\n");
 strFormat += _T("Content-Type:image/bmp");//
 strFormat += _T("\r\n");
 strFormat += _T("Content-Transfer-Encoding: binary");
 strFormat += _T("\r\n\r\n");
 strData.Format(strFormat, strBoundary, strFileName);
 return strData;
}


 

//封装协议尾
CString  CQueenMainDlg::MakePostFileData(CString &strBoundary)
{
 CString strFormat;
 CString strData;
 strFormat = _T("\r\n");
 strFormat += _T("--%s");
 strFormat += _T("\r\n");
 strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");
 strFormat += _T("\r\n\r\n");
 strFormat += _T("submit");
 strFormat += _T("\r\n");
 strFormat += _T("--%s--");
 strFormat += _T("\r\n");
 strData.Format(strFormat, strBoundary, strBoundary);
 
 return strData;
}


 

BOOL  CQueenMainDlg::SendTrack()
{
 
 CString m_Url = "http://192.168.1.66:8080/queen_web/imgupload/imgupload.do";// "http://192.168.1.13:65001/aaaa";//
 LoadSvrAddress(m_Url);
 CString m_strFilePath = "E:\\lobby1\\queen\\lobby\\";//theApp.m_strAppPath;
 m_strFilePath += _T("ScreenShot.bmp");

 CString strFileName = _T("ScreenShot.bmp");

 UpdateData(TRUE);
 CString defServerName =m_strSeverName;
 CString defObjectName =m_strObject;
 // USES_CONVERSION;
 CInternetSession Session;
 CHttpConnection *pHttpConnection = NULL;
 INTERNET_PORT   nPort = m_nPort;
 CFile fTrack;
 CHttpFile* pHTTP;
 CString strRequestHeader=_T("");
 CString strHTTPBoundary=_T("");
 CString strPreFileData=_T("");
 CString strPostFileData=_T("");
 CString strResponse =_T("");
 DWORD dwTotalRequestLength;
 DWORD dwChunkLength;
 DWORD dwReadLength;
 DWORD dwResponseLength;
 TCHAR szError[MAX_PATH];
 void* pBuffer =NULL;
 LPSTR szResponse;

 BOOL bSuccess = TRUE;

 CString strDebugMessage =_T("");

 if (FALSE == fTrack.Open(m_strFilePath, CFile::modeRead | CFile::shareDenyWrite))
 {
  AfxMessageBox(_T("Unable to open the file."));
  return FALSE;
 } 

 strHTTPBoundary = _T("-----------------------------7d86d16250370");
 strRequestHeader =MakeRequestHeaders(strHTTPBoundary);
 strPreFileData = MakePreFileData(strHTTPBoundary, strFileName);
 strPostFileData = MakePostFileData(strHTTPBoundary);

 MessageBox(strRequestHeader,"RequestHeader",MB_OK | MB_ICONINFORMATION);
 MessageBox(strPreFileData,"PreFileData",MB_OK | MB_ICONINFORMATION);
 MessageBox(strPostFileData,"PostFileData",MB_OK | MB_ICONINFORMATION);

 dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength()+ fTrack.GetLength();

 dwChunkLength = 64 * 1024;

 pBuffer = malloc(dwChunkLength);

 if (NULL == pBuffer)
 {
  return FALSE;
 }

 try
 {
  pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
  if(pHttpConnection == NULL)
  {
   throw 0 ; //连接服务器失败!
  }
  pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
  pHTTP->AddRequestHeaders(strRequestHeader);
  pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);

 

#ifdef _UNICODE
  pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
#else
  pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());
#endif

  dwReadLength = -1;
  int count = 1;
  
  while (0 != dwReadLength)
  {
   strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
   TRACE(strDebugMessage);
   dwReadLength = fTrack.Read(pBuffer, dwChunkLength);
   CString m_str;
   m_str.Format("count = %d,dwReadLength = %d\n",count,dwReadLength);
   count++;
   OutputDebugString(m_str);
   if (0 != dwReadLength)
   {
    pHTTP->Write(pBuffer, dwReadLength);
   }
  }

#ifdef _UNICODE
  pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());
#else
  pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif

  pHTTP->EndRequest(HSR_SYNC);

  dwResponseLength = pHTTP->GetLength();
  while (0 != dwResponseLength)
  {
   szResponse = (LPSTR)malloc(dwResponseLength + 1);
   szResponse[dwResponseLength] = '/0';
   pHTTP->Read(szResponse, dwResponseLength);
   strResponse += szResponse;
   free(szResponse);
   dwResponseLength = pHTTP->GetLength();
  }
  //MessageBox(strResponse,"Response",MB_OK | MB_ICONINFORMATION); 
 }
 catch (CException* e)
 {
  e->GetErrorMessage(szError, MAX_PATH);
  e->Delete();
  AfxMessageBox(szError);
  bSuccess = FALSE;
 }
 pHTTP->Close();
 delete pHTTP;

 fTrack.Close();

 if (NULL != pBuffer)
 {
  free(pBuffer);
 }
 return bSuccess;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值