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] = '\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(),,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") == )
{
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;
}
上一篇:Java条件查询涉及到时分秒


下一篇:.NET Core 控制台项目中的Nuget打包类库及引用