vc 读取网页内容
时间: 2023-07-18 18:19:33 浏览: 58
如果你想用 VC++ 读取网页内容,可以使用 WinINet 库提供的函数。以下是一个简单的示例代码:
```c++
#include <windows.h>
#include <wininet.h>
#include <iostream>
#pragma comment(lib, "wininet.lib")
int main()
{
HINTERNET hInternet = InternetOpen("MyUserAgent", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet)
{
std::cerr << "InternetOpen failed: " << GetLastError() << std::endl;
return 1;
}
HINTERNET hUrl = InternetOpenUrl(hInternet, "http://www.example.com", NULL, 0, 0, 0);
if (!hUrl)
{
std::cerr << "InternetOpenUrl failed: " << GetLastError() << std::endl;
InternetCloseHandle(hInternet);
return 1;
}
char buffer[1024];
DWORD bytesRead;
while (InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0)
{
std::cout.write(buffer, bytesRead);
}
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return 0;
}
```
这个示例代码使用 `InternetOpen` 函数打开一个与 Internet 的连接,然后使用 `InternetOpenUrl` 函数打开一个指定网址的 URL,最终使用 `InternetReadFile` 函数读取网页内容并输出到标准输出流。你可以根据需要修改这个代码。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)