利用WinInet依RFC1867提交表单

    CInternetSession m_Session("DigitalTitan");
    CHttpConnection* pServer=NULL;
    CHttpFile* pFile=NULL;
    CString strServerName=m_ServerName;
    INTERNET_PORT nPort=(INTERNET_PORT)atoi(m_ServerPort);
    CString strURL=m_DesignFile;
    try
    { 
 
        pServer=m_Session.GetHttpConnection(strServerName,nPort);
  //AfxMessageBox(strServerName,MB_ICONINFORMATION);
  pFile=pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strURL,NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
  //AfxMessageBox(strURL,MB_ICONINFORMATION);
  
  CString strBoundary="-----------------------------7d33a816d302b6";//分界线视CGI程序做响应改动.
        pFile->AddRequestHeaders("Content-Type: multipart/form-data, boundary="+strBoundary);
        pFile->AddRequestHeaders("Accept: **");
  
  char* pBuf=new char[1024*10000];
  
  //构造表单开始
  FieldStruct* pFieldStruct=new FieldStruct;
  POSITION CurrentPosition;
  for(int iIndex=0;iIndex<m_FieldData.GetCount();iIndex++)
  {
   CurrentPosition=m_FieldData.FindIndex(iIndex);
   pFieldStruct=(FieldStruct*)m_FieldData.GetAt(CurrentPosition);

   strcat(pBuf,(LPTSTR)(LPCTSTR)strBoundary);

   strcat(pBuf,"Content-Disposition: form-data;name=/"");
   strcat(pBuf,(LPTSTR)(LPCTSTR)pFieldStruct->m_FieldName);
   strcat(pBuf,"/"/r/n/r/n");
   strcat(pBuf,(LPTSTR)(LPCTSTR)pFieldStruct->m_FieldValue);
   strcat(pBuf,"/r/n");
   
   strcat(pBuf,(LPTSTR)(LPCTSTR)strBoundary);
  }
  //构造表单结束
  //  
  CStdioFile m_File;
  BYTE* pFileBuf=new BYTE[1024];
  CString strBuf;
  DWORD dwRead;
  strcat(pBuf,"Content-Disposition: form-data;name=/"FieldName/"/r/nfilename=/"FileName/"/r/n");
  //AfxMessageBox(pBuf,MB_ICONINFORMATION);
  if(m_File.Open(strTempDocPath,CFile::modeRead|CFile::typeBinary))
  {
   do
   {
    dwRead=m_File.Read(pFileBuf,1024);
    for(int kIndex=0;kIndex<(int)dwRead;kIndex++)
    {
     char strChar=*(char*)(pFileBuf+kIndex);
     if(strChar=='/0')
     {
      strBuf.Append("DigitalTitan",12);
     }
     else
     {
      strBuf.AppendChar(strChar);
     }
    }
   }
   while(dwRead>0);
   m_File.Close();
  }  
  //
  DeleteFile(strTempDocPath);
  strcat(pBuf,(LPTSTR)(LPCTSTR)strBuf);
  strcat(pBuf,"/r/n");
  strcat(pBuf,(LPTSTR)(LPCTSTR)strBoundary);
  strcat(pBuf,"--");
  //CString strBufLen;
  //strBufLen.Format("长度:%d",strlen(pBuf));
  //AfxMessageBox(strBufLen,MB_ICONINFORMATION);
  if(pFile->SendRequest(NULL,0,(LPVOID)pBuf,strlen(pBuf))==0)
  {
   AfxMessageBox("网络异常...",MB_ICONINFORMATION);
  }
  else
  {
  }
  pFile->Close();
  pServer->Close();
    }
    catch(CInternetException* e)
 {
  char strErrorBuf[255];
  e->GetErrorMessage(strErrorBuf,255,NULL);
  AfxMessageBox(strErrorBuf,MB_ICONINFORMATION);
 }
    delete pFile;
    delete pServer;
    m_Session.Close();

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值