Two ways to download files from a C + + server

Source: Internet
Author: User

#include <afxinet.h>
#include "wininet.h"


#pragma comment (lib, "wininet.lib")

StringRemoteip ="http://www.test.com:8001"; stringTestip = GetDomain ("IP"); stringTestport = GetDomain ("PORT"); if(testip! ="NULL"&& testport! ="NULL") {remoteip="/ http"+ Testip +":"+testport; } logs.addlog ("Current IP", remoteip); stringFinalURL = 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 =NewTinyxmlconfig (); stringversion; stringConfigxml = 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";//TwainLogs.addlog (finalurl); Try { //Write local XML stringTempxml = M_workdirection +"\\config\\temp.xml"; /* Get the XML file from the server 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; BOOLFlag =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 return value Hredult", To_string (urldown)); if(flag) {stringvalue; 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 ..."); returnFALSE; } if(HR = =TRUE) {logs.addlog ("isupdate () needupdate:", value); if(_STRICMP (value.c_str (),"true") ==0) {logs.addlog ("isupdate () needupdate ' s value is True"); returnTRUE; } Else{logs.addlog ("isupdate () needupdate ' value is flase"); returnFALSE; } } Else{logs.addlog ("isupdate () temp.xml getattribute Fail."); returnFALSE; } } } Catch(exception e) {logs.addlog ("isupdate () Exception"); returnFALSE; }

Two ways to download files from a C + + server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.