用CInternetSession实现HTTP POST登录

来源:互联网 发布:淘宝架设到公众号 编辑:程序博客网 时间:2024/06/07 16:17

最近,在编码时发现,原来使用第三方类库实现的HTTP登录验证对Session的处理不是很理想,于是在网上搜集了一些资料,尝试用 CInternetSession实现HTTP POST登录验证,代码如下:

 

        CString   strResultStr;
        CInternetSession   session; 
        CHttpConnection   *pConnection; 
        CHttpFile   *pFile;
        BOOL   retFlag; 

        CString strUserName = "jackongxp@163.com";

        CString strPassword = "666666";

 

        //与服务器建立连接
        pConnection   =   session.GetHttpConnection( "www.XXXXX.com" );    
        CString   strHeaders,   tempStr; 
        strHeaders   =   _T("Content-Type:   application/x-www-form-urlencoded"); 

 

        //登录页面为 www.XXXXX.com/Client/Login.aspx
        pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,    "/Client/Login.aspx?"); 


        CString strUserinfo;

        //在登录页面中主要有两个输入控件,为TextBoxUserName、TextBoxPassword,分别输入用户名和密码
        strUserinfo.Format( "TextBoxUserName=%s&TextBoxPassword=%s", strUserName, strPassword );   

        BOOL result   =   pFile->SendRequest( 
            strHeaders, 
            (LPVOID)(LPCTSTR)strUserinfo,   strUserinfo.GetLength() );      

 

        //将登录验证后服务器返回的信息放入strResultStr中

        do 
        { 
            pFile->SetReadBufferSize(10*1024); 
            retFlag   =   pFile->ReadString(tempStr); 
            strResultStr +=   tempStr   +   "/n"; 
        }while(retFlag   !=   FALSE);