CSDN博客

img gjd111686

利用WinInet依RFC1867提交表单

发表于2004/7/9 16:33:00  1775人阅读

分类: 经我试练的秘籍

    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();

0 0

相关博文

我的热门文章

img
取 消
img