• C++ 获取URL图片、html文件,CInternetSession 【转】


    http://blog.csdn.net/gnixuyil/article/details/7688439

    获取网络图片

    1. CString URL="http://www.google.com.hk/images/srpr/logo3w.png"  
    2.   
    3. CInternetSession session;  
    4. CHttpFile *httpFile = (CHttpFile *)session.OpenURL(URL);  
    5. CStdioFile imgFile;  
    6. char buff[1024];    // 缓存  
    7.   
    8. imgFile.Open("图片名字.png", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);  
    9.   
    10. DWORD dwStatusCode;  
    11. httpFile->QueryInfoStatusCode(dwStatusCode);  
    12.   
    13. if(dwStatusCode == HTTP_STATUS_OK) {  
    14.     int size=0;  
    15.     do {  
    16.         size = httpFile->Read(buff,1024);    // 读取图片  
    17.         imgFile.Write(buff,size);  
    18.     }while(size > 0);  
    19. }  
    20.   
    21. httpFile->Close();  
    22. session.Close();   


    获取URL的html

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


    编码转换类: http://blog.csdn.net/gnixuyil/article/details/7688469

    //下载文件并保存为新文件名

    #include <afx.h>
    #include <afxinet.h>
    #define RECVPACK_SIZE 2048
    bool DownloadSaveFiles(char* url,char *strSaveFile) {//下载文件并保存为新文件名
        bool ret=false;
        CInternetSession Sess("lpload");
        Sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT     , 2000); //2秒的连接超时
        Sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT        , 2000); //2秒的发送超时
        Sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT     , 2000); //2秒的接收超时
        Sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT   , 2000); //2秒的发送超时
        Sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 2000); //2秒的接收超时
        DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ;
     
        CHttpFile* cFile   = NULL;
        char      *pBuf    = NULL;
        int        nBufLen = 0   ;
        do {
            try{
                cFile = (CHttpFile*)Sess.OpenURL(url,1,dwFlag);
                DWORD dwStatusCode;
                cFile->QueryInfoStatusCode(dwStatusCode);
                if (dwStatusCode == HTTP_STATUS_OK) {
                    //查询文件长度
                    DWORD nLen=0;
                    cFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, nLen);
                    //CString strFilename = GetFileName(url,TRUE);
                    nBufLen=nLen;
                    if (nLen <= 0) break;//
     
                    //分配接收数据缓存
                    pBuf = (char*)malloc(nLen+8);
                    ZeroMemory(pBuf,nLen+8);
     
                    char *p=pBuf;
                    while (nLen>0) {
                        //每次下载8K
                        int n = cFile->Read(p,(nLen<RECVPACK_SIZE)?nLen:RECVPACK_SIZE);
                        //接收完成退出循环
                        if (n <= 0) break;//
                        //接收缓存后移
                        p+= n ;
                        //剩余长度递减
                        nLen -= n ;
                    }
     
                    //如果未接收完中断退出
                    if (nLen != 0) break;
     
                    //接收成功保存到文件
     
                    CFile file(strSaveFile, CFile::modeCreate | CFile::modeWrite);
                    file.Write(pBuf,nBufLen);
                    file.Close();
                    ret = true;
                }
            catch(...) {
                break;//
            }
        while(0);
     
        //释放缓存
        if (pBuf) {
            free(pBuf);
            pBuf=NULL;
            nBufLen = 0 ;
        }
     
        //关闭下载连接
        if (cFile) {
            cFile->Close();
            Sess.Close();
            delete cFile;
        }
        return ret;
    }
    int main() {
        DownloadSaveFiles("http://www.nirsoft.net/utils/nircmd.zip","d:\cppdld_nircmd.zip");
        return 0;
    }
  • 相关阅读:
    pytest.4.Fixture
    pytest.3.Assert
    pytest.2.运行多个文件
    [LeetCode 378.] Kth Smallest Element in a Sorted Matrix
    priority_queue 自定义 comparator
    原地调整法查找数组元素
    [LeetCode 436.] Find Right Interval
    [LeetCode 611.] Valid Triangle Number
    二叉树Morris遍历
    用户态IO:DPDK
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/5946203.html
Copyright © 2020-2023  润新知