使用wininet下载一个网页

WinInet基础知识 | Microsoft Learn

//this code excerpt also demonstrates try/catch exception handling

#include <afxinet.h>

void DisplayHttpPage(LPCTSTR pszServerName, LPCTSTR pszFileName)

{

CInternetSession session(_T("My Session")); //用于建立一个网络连接

CHttpConnection* pServer = NULL; //用于管理上面建立的网络连接

CHttpFile* pFile = NULL; //用于读取或者储存文件包括本地

try

{

CString strServerName;

INTERNET_PORT nPort = 80;

DWORD dwRet = 0;

pServer = session.GetHttpConnection(pszServerName, nPort); //这个就是常规用法即:

//CHttpConnectionObj=session.GetHttpConnection();

//后面这2个函数一起作用用于打开服务器上面的文件或者本地的文件

pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, pszFileName);

pFile->SendRequest();

pFile->QueryInfoStatusCode(dwRet);

if (dwRet == HTTP_STATUS_OK)

{

CHAR szBuff[1024];

while (pFile->Read(szBuff, 1024) > 0)

{

printf_s("%1023s", szBuff);

}

}

delete pFile;

delete pServer;

}

catch (CInternetException* pEx)

{

//catch errors from WinInet

TCHAR pszError[64];

pEx->GetErrorMessage(pszError, 64);

_tprintf_s(_T("%63s"), pszError);

}

session.Close();

}

相关推荐
不会写代码的里奇25 分钟前
深入解析ASR技术:从原理到C++高性能实现
c++
CSDN_RTKLIB41 分钟前
【类定义系列六】C++17新特性
开发语言·c++
hd51cc1 小时前
MFC文件操作
c++·mfc
春蕾夏荷_7282977252 小时前
Sockets-2.3.9.9 UDP使用实例
c++·udp
GetcharZp3 小时前
拒绝硬编码!C++ 配置文件管理神器 yaml-cpp 实战指南
c++
polarislove02143 小时前
9.6 [定时器]超声波测距实验-嵌入式铁头山羊STM32笔记
笔记·stm32·嵌入式硬件
墨有6663 小时前
C++ string 部分功能详解:迭代器、初始化与常用函数
开发语言·c++
枫叶丹43 小时前
【Qt开发】Qt系统(二)-> 事件分发器
c语言·开发语言·数据库·c++·qt·系统架构
一路往蓝-Anbo4 小时前
C语言从句柄到对象 (六) —— 继承与 HAL:父类指针访问子类数据
c语言·开发语言·stm32·嵌入式硬件·物联网
千里马-horse4 小时前
Rect Native bridging 源码分析--AString.h
c++·ts·rn·jsi