现在网络中html保存到文件也可以保存图片

来源:互联网 发布:java socket 百度云盘 编辑:程序博客网 时间:2024/06/04 01:29

有可能你看不懂,那就百度一下

头文件包含#include <afxinet.h>

如果地址是图片那就把文件名该文图片就可以了

int CTools::get_url_file(CString str_url,CString str_file){USES_CONVERSION;CInternetSession session;CHttpConnection* pHttpConnection = NULL;CHttpFile *pHttpFile = NULL;CString strServer, strObject;INTERNET_PORT wPort;DWORD dwType;if (!AfxParseURL(str_url, dwType, strServer, strObject, wPort)){//WriteLog(L"error.log", L"可能是你的配置文件接口没写,或者是接口格式有问题!");return 0;//URL解析错误}pHttpConnection = session.GetHttpConnection(strServer, wPort);pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);int result(-3);try{result = pHttpFile->SendRequest();}catch (CMemoryException* e){//WriteLog(L"error.log", L"内存出错!");}catch (CFileException* e){//WriteLog(L"error.log", L"打开文件失败!");}catch (CException* e){//WriteLog(L"error.log", L"请检查服务器接口地址是否可以访问?");}if (result == -3 || result == 0){//WriteLog(L"error.log", strURL + L"HTTP 请求失败");return -1;}DWORD dwRet;pHttpFile->QueryInfoStatusCode(dwRet);if (dwRet == HTTP_STATUS_OK){CFile file;file.Open(str_file, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone, 0);char *pszBuffer = new char[1024];UINT nRead = pHttpFile->Read(pszBuffer, 1024);//szpHtmlSoure = A2T(pszBuffer);*/while (nRead > 0){file.Write(pszBuffer, nRead);nRead = pHttpFile->Read(pszBuffer, 1024);}file.Close();delete[]pszBuffer;}else{CString stemp;stemp.Format(L"请求失败:%d 地址:", dwRet);//WriteLog(L"error.log", stemp + strURL);}if (pHttpFile != NULL){pHttpFile->Close();delete pHttpFile;pHttpFile = 0;}if (pHttpConnection != NULL){pHttpConnection->Close();delete pHttpConnection;pHttpConnection = 0;}session.Close();return dwRet;}


0 0
原创粉丝点击