// 真登录 void CLoginDlg::Login(BOOL prompt) { HRESULT result = GetCookie(m_cookie); TRACE(_T("0x%08X %s\n"), result, (LPCTSTR)m_cookie); if (FAILED(result)) { if (prompt) { CString tmp; if (result == ERROR_INVALID_DATA) tmp.Format(_T("无效的Cookie:\r\n%s"), (LPCTSTR)m_cookie); else tmp.Format(_T("获取Cookie失败!\r\n错误代码0x%08X\r\n"), result); AfxMessageBox(tmp, MB_ICONERROR); } return; } GetLoginUserName(); if (m_userName == _T("")) { AfxMessageBox(_T("获取用户名失败!"), MB_ICONERROR); return; } EndDialog(IDOK); }
// 使用IE Cookie void CLoginDlg::OnBnClickedButton3() { DWORD size = 1024 * 1024; BOOL result = InternetGetCookieEx(_T("http://tieba.baidu.com/"), NULL, m_cookie.GetBuffer(size), &size, INTERNET_COOKIE_HTTPONLY, NULL); m_cookie.ReleaseBuffer(); BOOL jump = TRUE; CheckResult: if (!result) { if (jump) goto Win10; AfxMessageBox(_T("获取Cookie失败!"), MB_ICONERROR); return; } if (!StringIncludes(m_cookie, _T("BDUSS="))) { if (jump) goto Win10; AfxMessageBox(_T("请先在IE浏览器登陆百度账号并选中下次自动登录!"), MB_ICONERROR); return; } GetLoginUserName(); if (m_userName == _T("")) { AfxMessageBox(_T("获取用户名失败!"), MB_ICONERROR); return; } AfxMessageBox(_T("登录完毕,不要在IE退出账号以免cookie失效,可以直接清除cookie"), MB_ICONINFORMATION); EndDialog(IDOK); return; Win10: size = 1024 * 1024; result = SUCCEEDED(IEGetProtectedModeCookie(L"http://tieba.baidu.com/", NULL, m_cookie.GetBuffer(size), &size, INTERNET_COOKIE_HTTPONLY)); jump = FALSE; goto CheckResult; }
// 登录 void CLoginDlg::OnOK() { CString userName, password, verifyCode; m_userNameEdit.GetWindowText(userName); if (userName == _T("")) { m_userNameEdit.SetFocus(); return; } m_passwordEdit.GetWindowText(password); if (password == _T("")) { m_passwordEdit.SetFocus(); return; } m_verifyCodeEdit.GetWindowText(verifyCode); if (verifyCode == _T("")) { m_verifyCodeEdit.SetFocus(); return; } EnableWindow(FALSE); time_t timestamp; time(×tamp); CString data; data.Format(_T("staticpage=http%%3A%%2F%%2Fwww.baidu.com%%2Fcache%%2Fuser%%2Fhtml%%2Fv3Jump.html&charset=utf-8&token=%s&tpl=mn") _T("&apiver=v3&tt=%I64d&codestring=%s&isPhone=false&safeflg=0&u=http%%3A%%2F%%2Fwww.baidu.com%%2F&username=%s&pass") _T("word=%s&verifycode=%s&mem_pass=on&ppui_logintime=35219&callback=parent.bd__pcbs__4y6hex"), m_token, timestamp, m_verifyStr, EncodeURI(userName), EncodeURI(password), verifyCode); CString result = HTTPPost(_T("https://passport.baidu.com/v2/api/?login"), data, TRUE, NULL, &m_cookie); EnableWindow(TRUE); if (result == NET_TIMEOUT_TEXT) { AfxMessageBox(_T("连接超时..."), MB_ICONERROR); OnStnClickedStatic4(); m_verifyCodeEdit.SetWindowText(_T("")); return; } if (!StringIncludes(m_cookie, _T("BDUSS="))) { WriteString(result, _T("login.txt")); AfxMessageBox(_T("登录失败!"), MB_ICONERROR); if (userName.Left(1) == _T("1")) AfxMessageBox(_T("(不能用手机号哦)"), MB_ICONINFORMATION); goto error; } GetLoginUserName(); if (m_userName == _T("")) { AfxMessageBox(_T("获取用户名失败!"), MB_ICONERROR); goto error; } CDialog::OnOK(); return; error: OnStnClickedStatic4(); m_verifyCodeEdit.SetWindowText(_T("")); }