C++ 获取URL图片、html文件,CInternetSession

获取网络图片
  1. CString URL=“http://192.168.0.23:8080/3DView/CR201505060107001.jpg”    
  2. CInternetSession session;    
  3. CHttpFile *httpFile = (CHttpFile *)session.OpenURL(URL);    
  4. CStdioFile imgFile;    
  5. char buff[1024];    // 缓存    
  6. imgFile.Open(”图片名字.png”, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);    
  7. DWORD dwStatusCode;    
  8. httpFile->QueryInfoStatusCode(dwStatusCode);    
  9. if(dwStatusCode == HTTP_STATUS_OK) {    
  10.     int size=0;    
  11.     do {    
  12.         size = httpFile->Read(buff,1024);    // 读取图片    
  13.         imgFile.Write(buff,size);    
  14.     }while(size > 0);    
  15. }    
  16. httpFile->Close();    
  17. session.Close();   
CString URL="http://192.168.0.23:8080/3DView/CR201505060107001.jpg"  
CInternetSession session;  
CHttpFile *httpFile = (CHttpFile *)session.OpenURL(URL);  
CStdioFile imgFile;  
char buff[1024];    // 缓存  
imgFile.Open("图片名字.png", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);  
DWORD dwStatusCode;  
httpFile->QueryInfoStatusCode(dwStatusCode);  
if(dwStatusCode == HTTP_STATUS_OK) {  
    int size=0;  
    do {  
        size = httpFile->Read(buff,1024);    // 读取图片  
        imgFile.Write(buff,size);  
    }while(size > 0);  
}  
httpFile->Close();  
session.Close(); 

获取URL的html

  1. CInternetSession session;    
  2. CHttpFile *httpFile = (CHttpFile *)session.OpenURL(m_URL);    
  3. DWORD dwStatusCode;    
  4. httpFile->QueryInfoStatusCode(dwStatusCode);    
  5. CString getdata=_T(”“);    
  6. if(dwStatusCode == HTTP_STATUS_OK) {    
  7.     CString line_data=_T(”“);    
  8.     while(httpFile->ReadString(line_data)) {     
  9.         getdata += line_data;          // 读取html    
  10.     }    
  11.     getdata.TrimRight();    
  12. }    
  13. httpFile->Close();   // html数据已经放在getdata中    
  14. session.Close();    
  15. // 如果 getdata 中保存的是UTF_8网页(可以看html的meta字段)    
  16. strCoding cfm;  // 编码转换类,详情请看下方连接    
  17. string temp = (LPCSTR)getdata.GetBuffer();  // 网页数据,转换成string型    
  18. string output;    
  19. // UTF_8转GB2312,让MFC控件能显示    
  20. cfm.UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data()));    
  21. // 若MFC字符集为Unicode的话,还需要将多字节转为宽字节    
  22. temp = output;    
  23. DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, NULL, 0);    
  24. wchar_t *pwText;    
  25. pwText = new wchar_t[dwNum];    
  26. MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, pwText, dwNum);    
  27. // 取得转换后结果 m_data 用于显示    
  28. m_data = pwText;    
  29. delete []pwText;   
CInternetSession session;  
CHttpFile *httpFile = (CHttpFile *)session.OpenURL(m_URL);  
DWORD dwStatusCode;  
httpFile->QueryInfoStatusCode(dwStatusCode);  
CString getdata=_T("");  
if(dwStatusCode == HTTP_STATUS_OK) {  
    CString line_data=_T("");  
    while(httpFile->ReadString(line_data)) {   
        getdata += line_data;          // 读取html  
    }  
    getdata.TrimRight();  
}  
httpFile->Close();   // html数据已经放在getdata中  
session.Close();  
// 如果 getdata 中保存的是UTF_8网页(可以看html的meta字段)  
strCoding cfm;  // 编码转换类,详情请看下方连接  
string temp = (LPCSTR)getdata.GetBuffer();  // 网页数据,转换成string型  
string output;  
// UTF_8转GB2312,让MFC控件能显示  
cfm.UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data()));  
// 若MFC字符集为Unicode的话,还需要将多字节转为宽字节  
temp = output;  
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, NULL, 0);  
wchar_t *pwText;  
pwText = new wchar_t[dwNum];  
MultiByteToWideChar (CP_ACP, 0, temp.c_str(), -1, pwText, dwNum);  
// 取得转换后结果 m_data 用于显示  
m_data = pwText;  
delete []pwText; 

DEMO:

  1. BOOL IHttpFileDownLoad::EnHttpImageDownLoad(CString strHttpUrl, CString strDownPath, CString &strDestFilePath)  
  2. {  
  3.     BOOL bFlag = FALSE;  
  4.     strHttpUrl.TrimLeft();  
  5.     strHttpUrl.TrimRight();  
  6.     if(strHttpUrl.IsEmpty() || strDownPath.IsEmpty()) return bFlag;  
  7.     if(!::PathFileExists(strDownPath)) return bFlag;  
  8.       
  9.     CString URL = strHttpUrl;  
  10.     CInternetSession session;   
  11.     CHttpFile *httpFile = NULL;  
  12.     try  
  13.     {  
  14.         httpFile = (CHttpFile *)session.OpenURL(URL);  
  15.     }  
  16.     catch (CInternetException* e)  
  17.     {  
  18.         e->Delete();  
  19.         return bFlag;  
  20.     }  
  21.     if(httpFile==NULL){  
  22.         session.Close();  
  23.         return bFlag;  
  24.     }  
  25.       
  26.     CString httpFileName = httpFile->GetFileName();  
  27.     CString strFilePath = strDownPath;  
  28.     if(strFilePath.Right(1)!=“\\”) strFilePath+=”\\”;  
  29.     strFilePath += httpFileName;  
  30.     CFileFind find;  
  31.     BOOL bFind = find.FindFile(strFilePath);  
  32.     if(bFind) CFile::Remove(strFilePath);  
  33.       
  34.     CStdioFile imgFile;   
  35.     if (imgFile.Open(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))  
  36.     {  
  37.         DWORD dwStatusCode;    
  38.         httpFile->QueryInfoStatusCode(dwStatusCode);    
  39.         if(dwStatusCode == HTTP_STATUS_OK) {    
  40.             bFlag = TRUE;  
  41.             DWORD fileLength = httpFile->GetLength();  
  42.             char *buff = new char[fileLength];// 缓存  
  43.             memset(buff, 0, fileLength);  
  44.             int size=0;    
  45.             do {    
  46.                 size = httpFile->Read(buff,1024);//读取图片    
  47.                 imgFile.Write(buff,size);    
  48.             }while(size > 0);    
  49.             if (buff!=NULL)  
  50.             {  
  51.                 delete [] buff;  
  52.                 buff = NULL;  
  53.             }  
  54.             strDestFilePath = strFilePath;  
  55.         }    
  56.     }  
  57.       
  58.     imgFile.Close();  
  59.     httpFile->Close();   
  60.     delete []httpFile;  
  61.     session.Close();    
  62.     return bFlag;  
  63. }  
BOOL IHttpFileDownLoad::EnHttpImageDownLoad(CString strHttpUrl, CString strDownPath, CString &strDestFilePath)
{
    BOOL bFlag = FALSE;
    strHttpUrl.TrimLeft();
    strHttpUrl.TrimRight();
    if(strHttpUrl.IsEmpty() || strDownPath.IsEmpty()) return bFlag;
    if(!::PathFileExists(strDownPath)) return bFlag;

    CString URL = strHttpUrl;
    CInternetSession session; 
    CHttpFile *httpFile = NULL;
    try
    {
        httpFile = (CHttpFile *)session.OpenURL(URL);
    }
    catch (CInternetException* e)
    {
        e->Delete();
        return bFlag;
    }
    if(httpFile==NULL){
        session.Close();
        return bFlag;
    }

    CString httpFileName = httpFile->GetFileName();
    CString strFilePath = strDownPath;
    if(strFilePath.Right(1)!="\\") strFilePath+="\\";
    strFilePath += httpFileName;
    CFileFind find;
    BOOL bFind = find.FindFile(strFilePath);
    if(bFind) CFile::Remove(strFilePath);

    CStdioFile imgFile; 
    if (imgFile.Open(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
    {
        DWORD dwStatusCode;  
        httpFile->QueryInfoStatusCode(dwStatusCode);  
        if(dwStatusCode == HTTP_STATUS_OK) {  
            bFlag = TRUE;
            DWORD fileLength = httpFile->GetLength();
            char *buff = new char[fileLength];// 缓存
            memset(buff, 0, fileLength);
            int size=0;  
            do {  
                size = httpFile->Read(buff,1024);//读取图片  
                imgFile.Write(buff,size);  
            }while(size > 0);  
            if (buff!=NULL)
            {
                delete [] buff;
                buff = NULL;
            }
            strDestFilePath = strFilePath;
        }  
    }

    imgFile.Close();
    httpFile->Close(); 
    delete []httpFile;
    session.Close();  
    return bFlag;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值