如何使用C++下载网页的HTML内容

How can I download the HTML contents of a webpage using C++

本文关键字:HTML 内容 网页 下载网 何使用 C++ 下载      更新时间:2023-10-16

这是我的代码

#include <iostream>
#include <Windows.h>
#include <WinInet.h>
using namespace std;
int main()
{
    HINTERNET hSession, hURL;
    char* Buffer = new char[1024];
    DWORD BufferLen, BytesWritten;
    HANDLE FileHandle;
    hSession = InternetOpenA(NULL, 0, NULL, NULL, 0);
    hURL = InternetOpenUrlA(hSession, "http://www.google.co.uk", NULL, 0, 0, 0);
    FileHandle = CreateFileA("C:\temp.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
    BytesWritten = 0;
    do
    {
        InternetReadFile(hURL, Buffer, 1024, &BufferLen);
        WriteFile(FileHandle, Buffer, BufferLen, &BytesWritten, NULL);
    } while (BufferLen != 0);
    CloseHandle(FileHandle);
    InternetCloseHandle(hURL);
    InternetCloseHandle(hSession);
    ShellExecuteA(0, "open", "C:\temp.txt", NULL, NULL, 1);
    cout << "Operation complete!";
    system("PAUSE");
    return 0;
}

以下是我遇到的错误

error LNK2019: unresolved external symbol __imp__InternetOpenA@20 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetCloseHandle@4 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetOpenUrlA@24 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetReadFile@16 referenced in function _main

我不明白我哪里错了。我已经导入了Wininet.h和Windows.h。为什么它仍然无法找到这些功能?当做

您所做的是包含头文件,但不链接到实现它的实际代码。

在项目中包含Wininet.lib。

项目->属性->配置属性->链接器->输入->附加依赖项