zoukankan      html  css  js  c++  java
  • C++服务器下载文件的两种方式

    
    

    #include <afxinet.h>
    #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] = ''; 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; }
  • 相关阅读:
    Idea快捷键
    基于LinkedHashMap实现一个基于LRU算法的缓存设计
    SpringBoot整合Mybatis
    MyBatis Generator自动生成代码
    java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
    Dubbo的异步调用
    Jackson的ObjectMapper的简单用法及语法
    mysql实例代码
    6. Z 字形变换
    13. 罗马数字转整数
  • 原文地址:https://www.cnblogs.com/mypsq/p/6944809.html
Copyright © 2011-2022 走看看