MFC WinInetHttp抓取网页代码内容

         Windows Internet编程主要包括两方面:

   l  服务器端

     l  客户端

WinInet编程

  Internet客户端主要实现的功能,主要是通过Internet协议(HTTP、FTP等)获取网络数据源(服务器)的信息。如,客户端可以访问服务器,获得天气预报、股票加个、新闻数据等信息。

  MFC为Internet客户端程序提供了专门的Win32 Internet扩展接口,即WinInet。

  在编写Wininet客户端程序时,可以直接调用Win32函数,也可以使用WinInet类库。

  WinInet为通用互联网协议(HTTP、FTP和Gopher)提供了统一的函数集,采用熟悉的Win32 API接口。

  WinInet实际上是包装在WinSock之上的,WinInet=WinSock+TCP/IP栈+Internet协议。

    

  使用WinInet进行客户端开发过程:

  1、  建立连接

  2、  发送请求

  3、  关闭连接

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
#include <afxinet.h>

CInternetSession InetSession;
CString strLine;
CInternetFile *pInetFile = 
NULL;
try
{
    pInetFile = (CInternetFile *)InetSession.OpenURL(strUrl);
}
catch (CInternetException *pException)
{
    pInetFile = 
NULL;
    pException->Delete();
}
CString strShowTxt;
char sRecived[1024] = {0};
if (pInetFile)
{
    
while (pInetFile->ReadString((LPTSTR)sRecived, 1024))
    {
        
/*
        LPCTSTR lpLine = strLine.GetBuffer(strLine.GetLength());
        int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,
                                              (LPCSTR)lpLine, -1,
                                              NULL, 0);
        wchar_t *pBuffer = new wchar_t[nBufferSize+1];
        MultiByteToWideChar(CP_UTF8, 0,
                           (LPCSTR)lpLine, -1 ,
                           pBuffer, nBufferSize*sizeof(wchar_t));
        strShowTxt = strShowTxt + (CString)pBuffer + _T(" ");
        free(pBuffer);
        */

        strShowTxt = strShowTxt + (CString)sRecived + _T(
" ");
    }
}
原文地址:https://www.cnblogs.com/MakeView660/p/6932411.html