加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-07-06 05:56:38 所属栏目:大数据 来源: https://www.jb51.cc
导读:最近实现了一个简单的CHttpSession类,用来请求制定URL的网页,支持POST传送方式和cookie机制。当然使用了CInternetSession,CHttpConnection和CHttpFile类,但我没有继承CInternetSession类。 POST支持的函数如下: BOOL CHttpSession::SendData(CHttpFile

最近实现了一个简单的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,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格式编码是在网上搜到的;
@H_502_301@

设置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);


}
 @H_502_301@

类的引用格式如下:
?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("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();
?}

?

?

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读