CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

急急急!在vc中如何用post方法提交表单

楼主glame(chen)2000-09-09 09:59:00 在 VC/MFC / 基础类 提问

本人在学vc的网络编程,遇一问题,在使用chttpfile类用post提交表单时,总是没有反应,我怀疑使用AddRequestHeaders();SendRequest();这两个函数不当,请具体解释一  
  下他们的使用方法(最好有个示例)和各参数的含义,万分感谢!!!!!! 问题点数:90、回复次数:3Top

1 楼westaf(开心灌水 快乐接分)回复于 2000-09-09 22:35:00 得分 40

我这里有一段程序,用来在一个对话框里显示出一次http   request的原始信息,不过使用Inet   API做的,希望能有帮助。  
   
  void   CHTTPRequestDlg::OnButtonRequest()    
  {  
  UpdateData(TRUE);  
  HINTERNET   hInternet   =   InternetOpen("Mozilla/4.0   (compatible;   MSIE   5.0;   Windows   NT   5.0)",   INTERNET_OPEN_TYPE_DIRECT,  
  NULL,   NULL,   NULL);  
  HINTERNET   hSession   =   InternetConnect(hInternet,   m_strHost,  
  m_nPort,   "username",   "password",  
  INTERNET_SERVICE_HTTP,   0,   0);  
  char*   szAccept[]   =   {"*/*",   NULL};  
  CString   strVerb;  
  m_comboVerb.GetWindowText(strVerb);  
  HINTERNET   hRequest   =   HttpOpenRequest(hSession,   strVerb,   m_strObject,   NULL,   NULL,   (LPCSTR*)szAccept,   0,   0);  
  struct  
  {  
  char*   Language;  
  char*   Encoding;  
  char*   ContentType;  
  }Headers   =   {"Accept-Language:   zh-cn\r\n",  
                  "Accept-Encoding:   gzip,   deflate\r\n",  
  "Content-Type:   application/x-www-form-urlencoded\r\n"};  
   
  if(m_bLanguage)  
  HttpAddRequestHeaders(hRequest,   Headers.Language,         -1,   HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);  
  if(m_bEncoding)  
  HttpAddRequestHeaders(hRequest,   Headers.Encoding,         -1,   HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);  
  if(m_bContentType)  
  HttpAddRequestHeaders(hRequest,   Headers.ContentType,   -1,   HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);  
  LPCSTR   lpAddHeader   =   NULL,   lpContent   =   NULL;  
  if(m_strHeaders.GetLength())  
  {  
  if(m_strHeaders.Right(2)   !=   "\r\n")  
  m_strHeaders   +=   "\r\n";  
  lpAddHeader   =   (LPCSTR)m_strHeaders;  
  }  
  if(m_strContent.GetLength()   &&   (strVerb   ==   "POST"   ||   strVerb   ==   "PUT"))  
  lpContent   =   (LPCSTR)m_strContent;  
  HttpSendRequest(hRequest,   lpAddHeader,   -1,   (LPVOID)lpContent,   m_strContent.GetLength());  
   
  m_editContentGot.SetSel(0,   -1);  
  m_editContentGot.ReplaceSel("");  
   
   
  LPSTR           lpszData; //   buffer   for   the   data  
  DWORD           dwSize;                               //   size   of   the   data   available  
  DWORD           dwDownloaded; //   size   of   the   downloaded   data  
   
  //   Set   the   cursor   to   an   hourglass.  
  SetCursor(LoadCursor(NULL,IDC_WAIT));  
   
  //   This   loop   handles   reading   the   data.      
  while(1)  
  {  
  //   The   call   to   InternetQueryDataAvailable   determines   the   amount   of    
  //   data   available   to   download.  
  if   (!InternetQueryDataAvailable(hRequest,&dwSize,0,0))  
  {  
  SetCursor(LoadCursor(NULL,IDC_ARROW));  
  break;  
  }  
  else  
  {            
  //   Allocates   a   buffer   of   the   size   returned   by   InternetQueryDataAvailable  
  lpszData   =   new   char[dwSize+1];  
   
  //   Reads   the   data   from   the   HINTERNET   handle.  
  if(!InternetReadFile(hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded))  
  {  
  delete[]   lpszData;  
  break;  
  }  
  else  
  {  
  //   Adds   a   null   terminator   to   the   end   of   the   data   buffer  
  lpszData[dwDownloaded]='\0';  
   
  int   nLen   =   m_editContentGot.GetWindowTextLength();  
  m_editContentGot.SetSel(nLen-1,   nLen-1);  
  m_editContentGot.ReplaceSel(lpszData);  
   
  //   Delete   the   two   buffers  
  delete[]   lpszData;  
   
  //   Check   the   size   of   the   remaining   data.     If   it   is   zero,   break.  
  if   (dwDownloaded   ==   0)  
  break;  
  }  
  }  
  }  
   
  //   Close   the   HINTERNET   handle  
  InternetCloseHandle(hRequest);  
  InternetCloseHandle(hSession);  
  InternetCloseHandle(hInternet);  
   
  //   Set   the   cursor   back   to   an   arrow  
  SetCursor(LoadCursor(NULL,IDC_ARROW));  
  }  
  Top

2 楼Sunlet(大赢家)回复于 2000-09-11 12:55:00 得分 40

使用MFC示例如下:  
  首先设置m_strRequest请求字符串     eg."name=aaa&pass=bbb";              
                  m_strServerName   服务器名称或者IP     eg."www.yahoo.com"  
                  m_strObjectName   请求文件位置   eg.   "pub/aaa.asp"  
      请求的结果存放在m_strHtml中  
  func(){  
                  CInternetSession   m_InetSession("session");    
  CHttpConnection*   pServer   =   NULL;  
  CHttpFile*   pFile   =   NULL;  
  try{  
  INTERNET_PORT   nPort;  
  nPort=80;  
  pServer   =   m_InetSession.GetHttpConnection(m_strServerName,   nPort);  
  pFile   =   pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,    
  m_strObjectName);  
                                  char   szHeaders[100];  
  strcpy(szHeaders,"Accept:   text*/*\r\nContent-Type:   application/x-www-form-urlencoded");  
  pFile->AddRequestHeaders(szHeaders);  
   
   
  pFile->SendRequestEx(m_strRequest.GetLength());  
  pFile->WriteString(m_strRequest);       //重要-->m_Request   中有"name=aaa&name2=BBB&..."  
  pFile->EndRequest();  
        DWORD   dwRet;  
  pFile->QueryInfoStatusCode(dwRet);  
  CString   str;  
   
  m_Mutex.Lock();  
  m_strHtml="";  
  char   szBuff[1024];  
  if   (dwRet   ==   HTTP_STATUS_OK){  
  UINT   nRead;  
  while   ((nRead   =   pFile->Read(szBuff,1023))>0)  
  {  
          m_strHtml+=CString(szBuff,nRead);  
          }  
  }  
  m_Mutex.Unlock();  
   
  delete   pFile;  
  delete   pServer;  
  }  
  catch   (CInternetException*   e){  
  CString   s;  
  s.Format("Internet   Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext);  
  AfxMessageBox(s);  
  //catch   errors   from   WinInet  
  }  
  }  
  Top

3 楼Sunlet(大赢家)回复于 2000-09-11 13:00:00 得分 10

这是从一个工程里面拷贝出来的.  
  上面程序中mutex的操作应该去处Top

相关问题

  • 100高分!急急,难题,有关用vc编写表单提交和IE的一些问题
  • vc++如何提交下面的表单?
  • 表单提交
  • 提交表单
  • 表单提交
  • 表单提交
  • 提交表单
  • 提交表单
  • 提交表单
  • 请问如何将表单的内容提交到Access数据库?急,急,急

关键词

  • idc
  • addreq
  • hinternet
  • hrequest
  • editcontentgot
  • lpszdata
  • strverb
  • dwdownloaded
  • lpaddheader
  • lpcontent

得分解答快速导航

  • 帖主:glame
  • westaf
  • Sunlet
  • Sunlet

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo