VC Http提交表单

void CUrlTestDlg::HttpPost(CString strUrl,CString strRequest)
{
 CString strServer;
 CString strObjectName;
 CInternetSession m_Session(_T("HttpClient")); 
 CHttpConnection * m_Server = NULL; 
 CHttpFile * m_File = NULL; 
 INTERNET_PORT     nPort = 0;
 DWORD   dwServerType = 0;

 if(AfxParseURL(strUrl,dwServerType,strServer,strObjectName,nPort))
 {
  CString strHeader =_T("Content-Type: application/x-www-form-urlencoded\r\n");
  strHeader += _T("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\n");
  try 
  { 
   m_Server = m_Session.GetHttpConnection(strServer,nPort); 
   m_File = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObjectName,NULL,1,(LPCSTR*)"Accept:*/*","HTTP/1.1");

   m_File->AddRequestHeaders(_T("Accept-Language: zh-cn"));

   m_File->AddRequestHeaders(_T("Accept-Charset:UTF-8\r\n")); 
   m_File->SendRequest(strHeader,(LPVOID)(LPCTSTR)strRequest,strRequest.GetLength());
 
   DWORD httpStatus; 
   m_File->QueryInfoStatusCode(httpStatus);  
   if (httpStatus == HTTP_STATUS_OK) 
   {  
    CString strLine; 
    CString strInfo;
    while(m_File->ReadString(strLine) > 0) 
     strInfo += strLine;

    if(strInfo.IsEmpty())
    {
     ::AfxMessageBox(_T("添加成功!"));
    }
    else
    {
     ::AfxMessageBox(strInfo);
    }
   } 
   else
   {
    ::AfxMessageBox(_T("添加失败!"));
   }
  } 
  catch (CInternetException* e) 
  { 
   e->ReportError();
   e->Delete();
  } 
  if(m_File) 
   delete m_File; 
  if (m_Server) 
   delete m_Server; 
  m_Session.Close();
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值