构建CHttpSession 类来实现网页数据的请求

最近实现了一个简单的CHttpSession类,用来请求制定URL的网页,支持POST传送方式和cookie机制。当然使用了CInternetSession,CHttpConnection和CHttpFile类,但我没有继承CInternetSession类。

POST支持的函数如下:

 
 
 
 
BOOL CHttpSession::SendData(CHttpFile * pFile,LPCSTR pszPostForm /*=NULL*/ ) { ASSERT(pFile!=NULL); BOOL bResult=TRUE; if(pszPostForm!=NULL) { CString strFormData(pszPostForm); //post data CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // URL-encoded form variables - bResult=pFile->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength()); TRACE("post data:%s/n",pszPostForm); } else { bResult=pFile->SendRequest(); } CString strReqHead,strRepHead; if(bResult) { //#ifdef _DEBUG DWORD dw=1; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,strRepHead); pFile->QueryInfoStatusCode(_nStatus); BOOL bResult=TRUE; bResult=pFile->QueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS|HTTP_QUERY_RAW_HEADERS_CRLF, strReqHead.GetBufferSetLength(dw),&dw,0); if(!bResult && GetLastError()==ERROR_INSUFFICIENT_BUFFER) { bResult=pFile->QueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS|HTTP_QUERY_RAW_HEADERS_CRLF, strReqHead.GetBufferSetLength(dw),&dw,0); } //szBuffer[dw]='/0'; //strReqHead=CString(szBuffer); //int nLen=strReqHead.ReverseFind('/n')+1; strReqHead.ReleaseBuffer(dw); //CString strTmp; //strTmp.Format("http req head:/n%s/n/n http rep head:/n%s",strReqHead,strRepHead); //TRACE(strTmp); //#endif TRACE("http req head:/n%s/n",strReqHead); TRACE("http rep head:/n%s/n",strRepHead); _strRepHead=strRepHead; _strReqHead=strReqHead; } else { TRACE("send req error!/n"); } return bResult; }
URL的form格式编码是在网上搜到的;

设置COOKIE相对简单些,直接调用CInternetSession类中的函数实现:

 
 
void CHttpSession::GetCookie(CString & strCookie,LPCTSTR pszUrl) { ASSERT(AfxIsValidString(pszUrl)); //cookie叠加 if(IsSameSite(_strUrl,pszUrl)) { TRACE("same site for cookie!/n"); } //得到存储的cookie strCookie=_T(""); DWORD dwCookieLen=CInternetSession::GetCookieLength(pszUrl,""); if(dwCookieLen>0) { BOOL bRet=CInternetSession::GetCookie(pszUrl, "", strCookie); //strCookie.ReleaseBuffer(2*dwCookieLen/sizeof(TCHAR)); TRACE("cookie=%s/n",strCookie); if(!bRet) { strCookie=_T(""); TRACE("得到cookie失败!/n"); } } _strUrl=CString(pszUrl); }

类的引用格式如下:
 CHttpSession httpSession;
 httpSession.SetStatusWnd(GetDlgItem(IDC_EDIT1)); //指定接收消息的窗口
 try{
  httpSession.SetHead(_T("Accept-Language"),_T("zh-cn"));
  httpSession.SetHead(_T("pragma"),_T("by luo31"));
  httpSession.SetHead(_T("Connection"),_T("Keep-Alive"));
  httpSession.SetHead(_T("Referer"),_T(http://www.sohu.com/login.html));

  httpSession.SetPost(_T("UserID"),_T("luo31"));
  httpSession.SetPost(_T("Password"),_T("iloveu"));
  httpSession.SetPost(_T("confirm"),_T("确 定"));
  httpSession.StartPostReq("http://www.sohu.com/login.html/login.asp"); 
  //AfxMessageBox(httpSession.GetReqHead());
  httpSession.EndReq();

  //go session
  httpSession.SetHead(_T("Accept-Language"),_T("zh-cn"));
  httpSession.SetHead(_T("pragma"),_T("by luo31"));
  httpSession.SetHead(_T("Connection"),_T("Keep-Alive"));
  httpSession.SetHead(_T("Referer"),_T("http://www.sohu.com/login.html/main.asp"));

  httpSession.StartGetReq("http://www.sohu.com/login.html/mail.asp"); 
   httpSession.SaveHtmlFile("mail.htm"); //存储目标文件
  //AfxMessageBox(httpSession.GetReqHead());
  httpSession.EndReq();  

  //ok
  AfxMessageBox("ok!");
}
 catch(CInternetException* e)
 {
  e->ReportError();
  e->Delete();
 }

 

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

webmote

如果能帮到你,请支持下博主

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值