win C/C++プログラムはGet方式でウェブソースコードを取得します。

5498 ワード

[回転]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
"), 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 }