win C/C++程序通过Get方式获取网页源代码

本文介绍了一个简单的C++程序示例,该程序利用Windows平台下的WinINet库进行HTTP请求并读取网页内容。代码展示了如何打开互联网会话、发送URL请求、读取响应数据及关闭连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[转自]http://www.cnblogs.com/coderzh/archive/2008/11/24/1340134.html

 1 #include <stdio.h>
 2 #include <windows.h>
 3 #include <wininet.h>
 4 
 5 #define MAXSIZE 1024
 6 #pragma comment(lib, "Wininet.lib") 
 7 
 8 void urlopen(_TCHAR*);
 9 
10 int _tmain(int argc, _TCHAR* argv[])
11 {
12     urlopen(_T("http://www.cnblogs.com/"));
13     getchar();
14     return 0;
15 }
16 
17 void urlopen(_TCHAR* url)
18 {
19     HINTERNET hSession = InternetOpen(_T("UrlTest"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
20     if(hSession != NULL)
21     {
22         HINTERNET hHttp = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
23 
24         if (hHttp != NULL)
25         {
26             wprintf_s(_T("%s\n"), url);
27 
28             char Temp[MAXSIZE];
29             ULONG Number = 1;
30             while (Number > 0)
31             {
32                 InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
33                 Temp[Number] = '\0';
34                 printf("%s", Temp);
35             }
36             InternetCloseHandle(hHttp);
37             hHttp = NULL;
38         }
39         InternetCloseHandle(hSession);
40         hSession = NULL;
41     } 
42 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值