C++服务器下载文件的两种方式


include

#include "wininet.h"

#pragma comment( lib, "wininet.lib" )string remoteIP = "http://www.test.com:8001";
    string testIP = getDomain("IP");
    string testPort = getDomain("PORT");
    if (testIP != "NULL" && testPort != "NULL")
    {
        remoteIP = "http://" + testIP + ":"+ testPort;
    }
    logs.addLog("当前IP", remoteIP);
    string finalURL = remoteIP + "/autoupdater.xml";
    Divice_info dinfo;
    finalURL += "?bit=";
    finalURL += dinfo.getSystemByte().c_str();
    finalURL += "&mem=";
    finalURL += dinfo.getMem_info().c_str();
    finalURL += "&disk=";
    finalURL += dinfo.getDisk_info().c_str();
    finalURL += "&os=";
    finalURL += dinfo.getOSVersion_info().c_str();
    finalURL += "&sys=";
    finalURL += dinfo.getSystem_info().c_str();
    finalURL += "&fingerprint=";
    finalURL += m_fingerprint;
    finalURL += "&version=";
    TinyXmlConfig *txc = new TinyXmlConfig();
    string version;
    string configxml = m_workdirection + "\\config\\config.xml";
    BOOL check = txc->getAttribute(configxml,"version", version);
    if (FALSE == check)
    {
        version = "0.0.0.0";
    }
    finalURL += version;
    finalURL += "&protocol=0";//twain


    logs.addLog(finalURL);
    try {
        //写入本地xml
        string tempxml = m_workdirection + "\\config\\temp.xml";
        /*
        //从服务器获取xml文件
        HINTERNET hOpen = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
        if (hOpen == NULL)
        {
            logs.addLog("isUpdate() Internet Open fail...");
            return FALSE;
        }
        HINTERNET hUrl = InternetOpenUrl(hOpen, finalURL.c_str(), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
        if (hUrl == NULL)
        {
            logs.addLog("isUpdate() Internet OpenUrl fail...");
            InternetCloseHandle(hOpen);
            return FALSE;
        }
        char buffer[10240];
        DWORD readByte = 0;
        DWORD writeByte = 0;
        BOOL hread, hwrite;
        buffer[readByte] = '\0';

        HANDLE createfile;
        createfile = CreateFile(tempxml.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (createfile == INVALID_HANDLE_VALUE)
        {
            logs.addLog("isUpdate() Create temp.xml failed!");
            return FALSE;
        }
        int flag = 0;
        while (true)
        {
            hread = InternetReadFile(hUrl, buffer, sizeof(buffer), &readByte);
            if (hread == FALSE)
            {
                logs.addLog("isUpdate() hUrl read buffer fail...");
                flag++;
                break;
            }
            if (readByte == 0)
            {
                logs.addLog("isUpdate() temp.xml writeed successful...");
                break;
            }
            hwrite = WriteFile(createfile, buffer, readByte, &writeByte, NULL);
            if (hwrite == FALSE)
            {
                flag++;
                logs.addLog("isUpdate() temp.xml write buffer fail...");
                break;
            }
        }
        CloseHandle(createfile);
        InternetCloseHandle(hOpen);
        InternetCloseHandle(hUrl);
        */
        //if (flag>0)
            //return FALSE;
        bool flag = false;
        HRESULT urldown = URLDownloadToFile(NULL,finalURL.c_str(), tempxml.c_str(),0,NULL);
        if (E_OUTOFMEMORY == urldown)
        {
            logs.addLog("server url xml is download fail");
        }
        else if (S_OK == urldown)
        {
            flag = true;
            logs.addLog("server url xml is dowload successful");
        }
        else if (INET_E_DOWNLOAD_FAILURE == urldown)
        {
            logs.addLog("server url xml is not exist");
        }
        logs.addLog("server url xml返回值HREDULT",to_string(urldown));
        if (flag)
        {
            string value;
            BOOL hr = txc->getAttribute(tempxml, "NeedUpdate", value);
            BOOL h1 = txc->getAttribute(tempxml, "url", m_urlZip);
            BOOL h2 = txc->getAttribute(tempxml, "lastver", m_lastVersion);
            BOOL h3 = txc->getAttribute(tempxml, "md5", m_md5);
            BOOL h4 = txc->getAttribute(tempxml, "size", m_filesize);
            delete txc;
            if (FALSE == h1 || FALSE == h2 || FALSE == h3 || FALSE == h4)
            {
                logs.addLog("isUpdate() temp.xml url or lastver or md5 or get size get fail...");
                return FALSE;
            }
            if (hr == TRUE)
            {
                logs.addLog("isUpdate() NeedUpdate : ", value);
                if (_stricmp(value.c_str(), "true") == 0)
                {
                    logs.addLog("isUpdate() NeedUpdate's value is True");
                    return TRUE;
                }
                else {
                    logs.addLog("isUpdate() NeedUpdate' value is Flase");
                    return FALSE;
                }
            }
            else {
                logs.addLog("isUpdate() temp.xml getAttribute fail..");
                return FALSE;
            }
        }
    }
    catch (exception e)
    {
        logs.addLog("isUpdate() Exception");
        return FALSE;
    }

原文链接: https://www.cnblogs.com/mypsq/p/6944809.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/254831

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午8:19
下一篇 2023年2月14日 上午8:19

相关推荐