【发布时间】:2018-07-06 03:35:02
【问题描述】:
我有一个将网页下载到文本文件中的功能
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <WinINet.h>
#pragma comment(lib, "WinINet.lib")
void Download(wstring url)
{
std::ofstream fout(L"temp.txt");
HINTERNET hopen = InternetOpen(L"MyAppName",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hopen)
{
DWORD flags = INTERNET_FLAG_DONT_CACHE;
if (url.find(L"https://") == 0)
flags |= INTERNET_FLAG_SECURE;
HINTERNET hinternet = InternetOpenUrl(hopen, url.c_str(), NULL, 0, flags, 0);
if (hinternet)
{
char buf[1024];
DWORD received = 0;
while (InternetReadFile(hinternet, buf, sizeof(buf), &received))
{
if (!received) break;
fout.write(buf, received);
}
InternetCloseHandle(hinternet);
}
InternetCloseHandle(hopen);
}
return;
}
当我给它“https://camelcamelcamel.com/Lodge-LMS3-Miniature-Skillet/product/B000LXA9YI”作为参数时,它只是输出 https://hastebin.com/gilomexomu.xml(太大放在这里) 这会切断大部分网页。我不确定网站上是否有一些反下载脚本,或者它是否太大。
【问题讨论】:
-
InternetReadFile 是否返回任何错误?
-
不,它没有......(由于 selbie,它已经解决了。)
标签: c++ windows winapi wininet