实现程序向网站的POST (MFC版)

本文提供了一个使用MFC WinInet类库模拟POST请求的示例代码,展示了如何通过C++向服务器发送表单数据,并读取服务器响应。此外,还提供了不依赖于MFC的SDK调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

下面给个简单的源码:

#include <afxinet.h>

CString strHeader = "Content-Type: application/x-www-form-urlencoded";
CString strDataFormat = "class_id=1";

CInternetSession session;
CHttpConnection* pConnection = session.GetHttpConnection("http://daids.wordpress.com.cn");
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, _T("/post.html?"));
BOOL result = pFile->SendRequest(strHeader, (LPVOID)(LPCTSTR)strDataFormat, strDataFormat.GetLength());
TCHAR* szBuffer = new TCHAR[65535];

int nlen = pFile->Read(szBuffer, 65535);

szBuffer[65534]= '/0';

CFile f;
f.Open("d://1.txt",CFile::modeCreate|CFile::modeWrite);
f.Write(szBuffer, 65535);
f.Close();

参考:

Here is a snippet of code that uses the MFC WinInet classes to simulate a Form POST request:
     CString strHeaders =
    
_T("Content-Type: application/x-www-form-urlencoded");
    
// URL-encoded form variables -
    
// name = "John Doe", userid = "hithere", other = "P&Q"
    
CString strFormData = _T("name=John+Doe&userid=hithere&other=P%26Q");
    
    
CInternetSession session;
    
CHttpConnection* pConnection =
    
session.GetHttpConnection(_T("ServerNameHere"));
    
CHttpFile* pFile =
    
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
    
_T("FormActionHere"));
    
BOOL result = pFile->SendRequest(strHeaders,
    
(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
    
    

     Without MFC, the same code translates to straight SDK calls as follows:
     static TCHAR hdrs[] =
    
_T("Content-Type: application/x-www-form-urlencoded");
    
static TCHAR frmdata[] =
    
_T("name=John+Doe&userid=hithere&other=P%26Q");
    
statuc TCHAR accept[] =
    
_T("Accept: */*");
    
    
// for clarity, error-checking has been removed
    
HINTERNET hSession = InternetOpen("MyAgent",
    
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    
HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
    
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
    
_T("FormActionHere"), NULL, NULL, accept, 0, 1);
    
HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    
// close any valid internet-handles

 CString   GetPageX(CString   m_svr,   CString   m_page,   CString   m_formdat)  
  {  
   
  CString   m_cont,//返回内容  
  m_err,//错误信息  
  s1;  
  //初始设置  
  CInternetSession   lc_isession;  
  lc_isession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000);  
  lc_isession.SetOption(INTERNET_OPTION_CONNECT_RETRIES,10);  
  lc_isession.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);  
  //连接  
  CHttpConnection   *pcon   =   lc_isession.GetHttpConnection(m_svr,80,"admin","password");  
  CString   FormDataType   =   _T("Content-Type:   application/x-www-form-urlencoded");  
  //m_formdat=MyReplace(m_formdat,'+');  
  //异常处理  
  try  
  {  
  CHttpFile   *http=pcon->OpenRequest("POST",m_page);  
  //SetCookie(m_cookie);  
  http->AddRequestHeaders("Accept:   image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/x-shockwave-flash,   application/msword,   */*");  
  http->AddRequestHeaders("Accept-Language:   zh-cn");  
  http->AddRequestHeaders("Content-Type:   application/x-www-form-urlencoded");  
  http->AddRequestHeaders("Accept-Encoding:   gzip,   deflate");  
  http->AddRequestHeaders("User-Agent:   Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   .NET   CLR   1.1.4322)");  
  http->AddRequestHeaders("Connection:   Keep-Alive");  
  http->AddRequestHeaders("Cache-Control:   no-cache");  
  //http->AddRequestHeaders("Cookie:   FirstLogon=D195593569A134;");  
  http->SendRequest(FormDataType,(LPVOID)(LPCTSTR)m_formdat,m_formdat.GetLength());  
  //lc_isession.GetCookie(m_cookie);  
  //AfxMessageBox(m_cookie);  
  CString   mstr;  
  m_cont.Empty();  
  while(http->ReadString(mstr))  
  {  
  m_cont+=mstr+"/n";  
  }  
  m_cont.Replace("<br>","/r/n");  
  //m_cont即保存了返回内容  
  http->Close();  
  pcon->Close();  
  //AfxMessageBox(m_cont);  
  return   m_cont;  
  }  
  catch   (CInternetException*   pEx)  
  {  
  char   mcharerr[100];  
  pEx->GetErrorMessage(mcharerr,100);  
  m_err=mcharerr;  
  return   m_cont;  
  }  
  }

CString   GetPageX(CString   m_svr,   CString   m_page,   CString   m_formdat)  
   
  其中的    
  m_svr是类似于这样的数据:www.163.com  
  m_page是类似于这样的数据:/login.asp  
  m_formdat   是类似于这样的表单数据:  
  name=yang&password=123

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值