bool HTTP_Post_an_Get(const wchar_t *url, std::wstring &OutHeaders,std::string &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, &urlComp)) {
printf("Error %u in WinHttpCrackUrl.\n", GetLastError());
return false;
}
wchar_t HostNameStr[64];
ZeroMemory(HostNameStr, 64 * 2);
//printf("lpszHostName:%S\n", urlComp.lpszHostName);
//printf("lpszHostName:%u\n", urlComp.dwHostNameLength);
CopyMemory(HostNameStr, urlComp.lpszHostName, urlComp.dwHostNameLength * 2);
//printf("lpszHostName:%S\n", HostNameStr);
//printf("lpszUrlPath:%S\n", urlComp.lpszUrlPath);
//printf("nPort:%u\n", 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.\n", GetLastError());
return false;
}
HINTERNET hConnect = WinHttpConnect(hSession, HostNameStr,
urlComp.nPort, 0);
if (!hConnect) {
printf("Error %u in WinHttpConnect.\n", 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.\n", GetLastError());
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
if (Redirection) {
DWORD rt = WINHTTP_DISABLE_REDIRECTS;
if (!WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, &rt, 4)) {
printf("Error %u in WinHttpSetOption.\n", 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, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize + 1);
//printf("%u\n", dwSize);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n", GetLastError());
else
Data += std::string((char*)pszOutBuffer);
//printf("%s\n", 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.\n", GetLastError());
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
//printf("Headers:%S\n", (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;
//获取分享ID
HTTP_Post_an_Get(shareUrl, xyt, data, true);
int np = xyt.find(L"id=");
std::wstring FileId = xyt.substr(np + 3, 32);
//获取文件ID 多个文件
std::wstring DataUrl = L"https://note.youdao.com/yws/public/notebook/" + FileId;
HTTP_Post_an_Get(DataUrl.c_str(), xyt, data, false);
//JSON解析数据
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\n", root[2][i]["p"].asString().substr(36).c_str());
//printf("WEB:%s\n", root[2][i]["p"].asString().c_str());
//printf("文件名:%s\n", root[2][i]["tl"].asString().c_str());
///WEB914f4422aaba9d369433ce08c5e7cf04/WEBacbb3c563193d9277204d08c1c1d7435
}
return FileNum;
}
return 0;
}
源码下载