如何用winsock登录网页并保持登录状态

解决方案 »

  1.   

    发送一个登陆的POST数据包就可以了。
    然后把SESSION保存下来。
      

  2.   

    保留session or cookie就行
      

  3.   

    一点建议,可以看看下面几个类,HTTP操作比WinSock方便多了。 CInternetSession
    CHttpConnection
    CHttpFile/******下面是一个大概的例子***************/
    CInternetSession  *m_pInnetSession;
    CHttpConnection  *m_pHttpConnection;
    CHttpFile  *m_pHttpFile;//Step1:初始化
    int CMyGetWebData::InitWebSession(CString szServerName, int iPort)
    {
    //create a new internet session
    m_pInnetSession = new CMyInterSession();

    //enable call back function, so can handle some message
    //m_pInnetSession->EnableStatusCallback();

    //init m_pInnetSession, tell it my instance
    //m_pInnetSession->Init(this); try
    {
    //then open to a server,and return a http connection
    m_pHttpConnection = m_pInnetSession->GetHttpConnection((LPCTSTR)szServerName, INTERNET_FLAG_RELOAD, (INTERNET_PORT)iPort);
    if(m_pHttpConnection == NULL)
    {
    //have some error
    return -1;
    }
    }
    catch(CInternetException *pExcp)
    {
    //have some exception, handle it, then delete, waiwai
    pExcp->Delete();
    return -1;
    } return 0;
    }//Step2:发送请求,我当时写的是用GET方法。可以携带任何Request头。
    /****
    采用GET方式获得一个网页数据
    szGetStr: The Get URL string(not include 'get')
    ****/
    int CMyGetWebData::SendGetRequest(CString szGetStr)
    {
    try
    {
    m_pHttpFile = m_pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, (LPCTSTR)szGetStr, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD);

    //增加各种Request Header
    m_pHttpFile->AddRequestHeaders( theApp.m_constAccept);
    m_pHttpFile->AddRequestHeaders( theApp.m_constAcceptLang);
    m_pHttpFile->AddRequestHeaders( theApp.m_constAcceptEncode);
    m_pHttpFile->AddRequestHeaders( theApp.m_constUserAgent);

    m_pHttpFile->SendRequest();

    #if 0
    //handle data
    if(HandleWebFileFirst() != 0)
    {
    return -1;
    }
    #endif
    }
    catch(CInternetException *pExcp)
    {
    //have some exception, handle it, then delete, waiwai
    pExcp->Delete();
    return -1;
    } return 0;
    }//Step3:收到HTML文件之后处理。
    /*对HTML文件进行处理*/
    int CMyGetWebData::HandleWebFile(void)
    { //check return code
    if(m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, iRetCode) == 0)
    {
    //have some error
    iErrCode = GetLastError();
    goto Err_Exit;
    }
    /*
    else
    {
    iRetCode = 0;
    }
    */

    if(iRetCode != 200)
    {
    CString cRetText;
    //not OK , can record some reason
    m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, cRetText);
    goto Err_Exit;
    } //success begin handle //获得文件长度
    m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dwHttpFileSize);
    if( dwHttpFileSize == 0)
    {
    //查询得到的页面不对,没有查询到相应的内容
    ASSERT(FALSE);
    goto Err_Exit;
    }
            ...
    }
      

  4.   

    为什么要用winsock?winhttp/wininet都支持http协议
      

  5.   

    winsock有很多优点啊
    比如最讨厌的wininet超时问题
    比如绕过http-only等等
      

  6.   

    也可以直接用Webbroser试试,我最近也在做这个
      

  7.   

    推荐CHttpFile  这样不用管理cookie  
    最近用C#重写  cookie太麻烦了...  
      

  8.   

    保存cookie就可以,libcurl可以实现