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

时间:2014-10-08 16:35:29

标签: c++ wininet

这是我的代码

#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。为什么它仍然无法找到这些功能?问候。

1 个答案:

答案 0 :(得分:0)

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

在项目中包含Wininet.lib。

项目 - &gt;属性 - &gt;配置属性 - &gt;链接器 - &gt;输入 - &gt;其他依赖关系