天天看点

vc 获取网页内容(HTTP)

BOOL bResult = FALSE;

    // 初始化WinInet 环境    

    HINTERNET hInternet = InternetOpen("CEHTTP", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);

    if(hInternet == NULL)

        return ;

    // 打开http session    

    HINTERNET hSession = InternetConnect(hInternet, "www.hao123.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

    if(hSession == NULL)

    // 打开http post 请求的句柄

    char* szAccept[] = {"*/*", NULL};

    HINTERNET hRequest = HttpOpenRequest(hSession, "GET", NULL, 

        NULL, NULL, (LPCSTR*)szAccept, INTERNET_FLAG_NO_CACHE_WRITE, 0); 

    if (hRequest == NULL)

    // 外发的header

    char headerLanguage[] = "Accept-Language: zh-cn/r/n";    

    char headerEncoding[] = "Accept-Encoding: gzip, deflate/r/n";    

    char headerContentType[] = "Content-Type: text/xml/r/n";    

    // 添加header 信息    

//     bResult = HttpAddRequestHeaders(hRequest, headerLanguage,    -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);    

//     bResult = HttpAddRequestHeaders(hRequest, headerEncoding,    -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);    

//     bResult = HttpAddRequestHeaders(hRequest, headerContentType, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);    

//     

    if(!HttpSendRequest(hRequest,NULL,0,NULL,0))

    TCHAR    szBuf[256];

    DWORD    dwRet = -1;

    while (InternetReadFile(hRequest,szBuf,256,&dwRet) && dwRet!= 0)

    {

        OutputDebugString(szBuf);

    }