/*! *\overload */ bool Sodaq_WifiBee::HTTPPost(const String& server, const uint16_t port, const String& URI, const String& headers, const String& body, uint16_t& httpCode) { return HTTPPost(server.c_str(), port, URI.c_str(), headers.c_str(), body.c_str(), httpCode); }
// 检查更新 CheckUpdateResult CheckUpdate() { static const wregex CHECK_UPDATE_REG(_T("\"fs_id\":(\\d+),\"path\":\"\\\\/\\\\u6211\\\\u7684\\\\u5206\\\\u4eab\\\\/\\\\u767e") _T("\\\\u5ea6\\\\u8d34\\\\u5427\\\\u76f8\\\\u5173\\\\/\\\\u8d34\\\\u5427\\\\u7ba1\\\\u7") _T("406\\\\u5668.zip\",\"server_filename\":\"\\\\u8d34\\\\u5427\\\\u7ba1\\\\u7406\\\\u5") _T("668.zip\".*?\"server_ctime\":(\\d+),")); CString src = HTTPGet(_T("http://pan.baidu.com/share/list?channel=chunlei&clienttype=0&web=1&num=100&page=1&dir=%2F%E6%88%91") _T("%E7%9A%84%E5%88%86%E4%BA%AB%2F%E7%99%BE%E5%BA%A6%E8%B4%B4%E5%90%A7%E7%9B%B8%E5%85%B3&uk=436464474&") _T("shareid=497149087"), FALSE); std::wcmatch res; if (!std::regex_search((LPCTSTR)src, res, CHECK_UPDATE_REG)) return UPDATE_FAILED_TO_GET_FILE_ID; // 文件ID CString fs_id = res[1].str().c_str(); // 上传时间戳 CString server_ctime = res[2].str().c_str(); time_t tServer_ctime = _ttoi64(server_ctime); tm tmServer_ctime; localtime_s(&tmServer_ctime, &tServer_ctime); // 上传时间 CString time; time.Format(_T("%02d-%02d-%02d"), tmServer_ctime.tm_year % 100, tmServer_ctime.tm_mon + 1, tmServer_ctime.tm_mday); if (time == UPDATE_CURRENT_VERSION) return UPDATE_NO_UPDATE; if (AfxMessageBox(_T("最新版本") + time + _T(",是否更新?"), MB_ICONQUESTION | MB_YESNO) == IDNO) return UPDATE_HAS_UPDATE; static const TCHAR TIME_STAMP_LEFT[] = _T("yunData.TIMESTAMP = \""); static const TCHAR TIME_STAMP_RIGHT[] = _T("\""); static const TCHAR SIGN_LEFT[] = _T("yunData.SIGN = \""); static const TCHAR SIGN_RIGHT[] = _T("\""); static const TCHAR BDSTOKEN_LEFT[] = _T("yunData.MYBDSTOKEN = \""); static const TCHAR BDSTOKEN_RIGHT[] = _T("\""); static const TCHAR DLINK_LEFT[] = _T("\"dlink\":\""); static const TCHAR DLINK_RIGHT[] = _T("\""); src = HTTPGet(UPDATE_URL, FALSE); CString timeStamp = GetStringBetween(src, TIME_STAMP_LEFT, TIME_STAMP_RIGHT); CString sign = GetStringBetween(src, SIGN_LEFT, SIGN_RIGHT); CString bdstoken = GetStringBetween(src, BDSTOKEN_LEFT, BDSTOKEN_RIGHT); CString url; url.Format(_T("http://pan.baidu.com/api/sharedownload?sign=%s×tamp=%s&bdstoken=%s&channel=chunlei") _T("&clienttype=0&web=1&app_id=250528"), sign, timeStamp, bdstoken); src = HTTPPost(url, _T("encrypt=0&product=share&uk=436464474&primaryid=497149087&fid_list=%5B") + fs_id + _T("%5D"), FALSE); // 文件下载链接 CString dlink = GetStringBetween(src, DLINK_LEFT, DLINK_RIGHT); if (dlink == _T("")) return UPDATE_FAILED_TO_GET_LINK; dlink.Replace(_T("\\/"), _T("/")); ShellExecute(NULL, _T("open"), dlink, NULL, NULL, SW_NORMAL); AfxMessageBox(_T("下载后解压并覆盖旧版本文件,可保留当前设置"), MB_ICONINFORMATION); return UPDATE_HAS_UPDATE; }
// 登录 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("")); }
json_spirit::Object CallRPC(const std::string& strMethod, const json_spirit::Array& params) { if (mapArgs["-rpcuser"] == "" && mapArgs["-rpcpassword"] == "") throw std::runtime_error(strprintf( _("You must set rpcpassword=<password> in the configuration file:\n%s\n" "If the file does not exist, create it with owner-readable-only file permissions."), GetConfigFile().string().c_str())); // Connect to localhost bool fUseSSL = GetBoolArg("-rpcssl", false); boost::asio::io_service io_service; boost::asio::ssl::context context(io_service, boost::asio::ssl::context::sslv23); context.set_options(boost::asio::ssl::context::no_sslv2); boost::asio::ssl::stream<boost::asio::ip::tcp::socket> sslStream(io_service, context); SSLIOStreamDevice<boost::asio::ip::tcp> d(sslStream, fUseSSL); boost::iostreams::stream< SSLIOStreamDevice<boost::asio::ip::tcp> > stream(d); bool fWait = GetBoolArg("-rpcwait", false); // -rpcwait means try until server has started do { bool fConnected = d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(Params().RPCPort()))); if (fConnected) break; if (fWait) MilliSleep(1000); else throw std::runtime_error("couldn't connect to server"); } while (fWait); // HTTP basic authentication std::string strUserPass64 = EncodeBase64(mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"]); std::map<std::string, std::string> mapRequestHeaders; mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64; // Send request std::string strRequest = JSONRPCRequest(strMethod, params, 1); std::string strPost = HTTPPost(strRequest, mapRequestHeaders); stream << strPost << std::flush; // Receive HTTP reply status int nProto = 0; int nStatus = ReadHTTPStatus(stream, nProto); // Receive HTTP reply message headers and body std::map<std::string, std::string> mapHeaders; std::string strReply; ReadHTTPMessage(stream, mapHeaders, strReply, nProto); if (nStatus == HTTP_UNAUTHORIZED) throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR) throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus)); else if (strReply.empty()) throw std::runtime_error("no response from server"); // Parse reply json_spirit::Value valReply; if (!read_string(strReply, valReply)) throw std::runtime_error("couldn't parse reply from server"); const json_spirit::Object& reply = valReply.get_obj(); if (reply.empty()) throw std::runtime_error("expected reply to have result, error and id properties"); return reply; }