可以使用 CInternetSession类发送HTTP请求获取网页HTML代码。示例代码如下(strHtml用于存储HTML代码):CInternetSessionsession;CHttpFile*file=NULL;CStringstrURL="http://www.baidu.com";CStringstrHtml="”;//存放网页数据try{

可以使用 CInternetSession类发送HTTP请求获取网页HTML代码。

C++发送HTTP请求获取网页HTML代码  第1张

示例代码如下(strHtml用于存储HTML代码):

CInternetSession session;CHttpFile *file = NULL;CString strURL = " http://www.baidu.com";CString strHtml = "”; //存放网页数据try{ file = (CHttpFile*)session.OpenURL(strURL);}catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); MessageBox("CInternetException");}CString strLine;if(file != NULL){ while(file->ReadString(strLine) != NULL){ strHtml += strLine; } }else{ MessageBox("fail");}session.Close();file->Close();delete file;file = NULL;

转载请说明出处
知优网 » C++发送HTTP请求获取网页HTML代码

发表评论

您需要后才能发表评论