MFC的HTTP客户端
一、建立会话(Session)对象:
CInternetSession mysession;
二、连接到Http服务器:
CHttpConnection *myconn=mysession.GetHttpConnection("www.baidu.com");
三、打开Http请求:
CHttpFile *myfile=myconn->OpenRequest("GET","/index.html");
四、发送Http请求:
myfile->SendRequest();
五、从服务器读取字节流(bytes):
CString mystr;
CString tmp;
while(myfile->ReadString(tmp))
{
mystr+=tmp;
}
六、释放资源:
myfile->Close();
myconn->Close();
mysession.Close();
delete myfile;
delete myconn;
myfile=0;
myconn=0;
步骤基本如上,上例各函数参数主要使用默认参数,具体函数声明请查阅MSDN。另外别忘了异常处理!
1、下载文件
Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
ASSERT(strFileURLInServer != "");
ASSERT(strFileLocalFullPath != "");
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
DWORD dwType;
const int nTimeOut = 2000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,
原文地址:https://blog.csdn.net/m0_37346206/article/details/143477651
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!