bool HTTP_Post_an_Get(const wchar_t *url, std::wstring & amp;OutHeaders,std::string & amp;Data, bool Redirection = false ) { DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; LPVOID lpOutBuffer = NULL; BOOL bResults = FALSE; URL_COMPONENTS urlComp; ZeroMemory( &urlComp, sizeof(urlComp)); urlComp.dwStructSize = sizeof(urlComp); urlComp.dwSchemeLength = (DWORD)-1; urlComp.dwHostNameLength = (DWORD)-1; urlComp.dwUrlPathLength = (DWORD)-1; urlComp.dwExtraInfoLength = (DWORD)-1; if (!WinHttpCrackUrl(url, wcslen(url), 0, & amp;urlComp)) { printf("Error %u in WinHttpCrackUrl.\ ", GetLastError()); return false; } wchar_t HostNameStr[64]; ZeroMemory(HostNameStr, 64 * 2); //printf("lpszHostName: %S\ ", urlComp.lpszHostName); //printf("lpszHostName:%u\ ", urlComp.dwHostNameLength); CopyMemory(HostNameStr, urlComp.lpszHostName, urlComp.dwHostNameLength * 2); //printf("lpszHostName: %S\ ", HostNameStr); //printf("lpszUrlPath: %S\ ", urlComp.lpszUrlPath); //printf("nPort: %u\ ", urlComp.nPort); // Use WinHttpOpen to obtain an HINTERNET handle. HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (!hSession) { printf("Error %u in WinHttpOpen.\ ", GetLastError()); return false; } HINTERNET hConnect = WinHttpConnect(hSession, HostNameStr, urlComp.nPort, 0); if (!hConnect) { printf("Error %u in WinHttpConnect.\ ", GetLastError()); WinHttpCloseHandle(hSession); return false; } HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", urlComp.lpszUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); if (!hRequest) { printf("Error %u in WinHttpOpenRequest.\ ", GetLastError()); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); return false; } if (Redirection) { DWORD rt = WINHTTP_DISABLE_REDIRECTS; if (!WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, & amp;rt, 4)) { printf("Error %u in WinHttpSetOption.\ ", GetLastError()); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); return false; } } bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) { bResults = WinHttpReceiveResponse(hRequest, NULL); if (bResults) { do { // Check for available data. dwSize = 0; if (!WinHttpQueryDataAvailable(hRequest, & amp;dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\ ", GetLastError()); //Allocate space for the buffer. pszOutBuffer = new char[dwSize + 1]; if (!pszOutBuffer) { printf("Out of memory\ "); dwSize = 0; } else { // Read the Data. ZeroMemory(pszOutBuffer, dwSize + 1); //printf("%u\ ", dwSize); if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\ ", GetLastError()); else Data + = std::string((char*)pszOutBuffer); //printf("%s\ ", pszOutBuffer); // Free the memory allocated to the buffer. //Data = std::wstring((wchar_t*)pszOutBuffer); delete[] pszOutBuffer; } } while (dwSize > 0); WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX); //Allocate memory for the buffer. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)]; // Now, use WinHttpQueryHeaders to retrieve the header. bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, lpOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX); } if (!bResults) { printf("Error %u in WinHttpQueryHeaders.\ ", GetLastError()); WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); return false; } //printf("Headers:%S\ ", (wchar_t*)lpOutBuffer); OutHeaders = std::wstring((wchar_t*)lpOutBuffer); delete[] lpOutBuffer; } } // Close any open handles. if (hRequest) WinHttpCloseHandle(hRequest); if (hConnect) WinHttpCloseHandle(hConnect); if (hSession) WinHttpCloseHandle(hSession); return false; }
int YouDaoYunJieXiFile(const wchar_t *shareUrl,std::vector<std::string> *LoaddownUrl,std::vector<std::string> *pFileName) { std::wstring xyt; std::string data; //Get share ID HTTP_Post_an_Get(shareUrl, xyt, data, true); int np = xyt. find(L"id="); std::wstring FileId = xyt. substr(np + 3, 32); //Get the file ID for multiple files std::wstring DataUrl = L"https://note.youdao.com/yws/public/notebook/" + FileId; HTTP_Post_an_Get(DataUrl.c_str(), xyt, data, false); //JSON parsing data Json::Reader reader; Json::Value root; if (reader. parse(data. c_str(), root)) { int FileNum = root[0].asInt(); std::string FileName = root[1].asString(); for (int i = 0; i < FileNum; i ++ ) { std::string downurl = "https://note.youdao.com/yws/api/personal/file" + root[2][i]["p"].asString().substr(36) + "? method=download & shareKey=" + WString2String(FileId); LoaddownUrl->push_back(downurl); if (pFileName != NULL) { std::string dfliename = root[2][i]["tl"].asString(); pFileName->push_back(dfliename); } //printf("WEB:%s\ ", root[2][i]["p"].asString().substr(36).c_str()); //printf("WEB:%s\ ", root[2][i]["p"].asString().c_str()); //printf("File name: %s\ ", root[2][i]["tl"].asString().c_str()); ///WEB914f4422aaba9d369433ce08c5e7cf04/WEBacbb3c563193d9277204d08c1c1d7435 } return FileNum; } return 0; }
Source code download