Void downloadfrominternet (System: String URL, system: String path) {// create the httprequest request object system: Net: httprequest * request = new system: Net :: httprequest (URL); // If memory permits, the returned pointer is always available if (! Request) return; // initialize the request to meet our requirements. Request-> method = T ("get"); Request-> timeout = 8000; request-> useragent = T ("rlib downloader/1.1"); Request-> Accept = T ("text/XML, application/XML, application/XHTML + XML, text/html; */* "); Request-> headers. add ("Accept-charset", "iso-8859-1, UTF-8; q = 0.7, *; q = 0.7"); Request-> headers. add ("Accept-language", "ZH-CN, ZH; q = 1.0, en; q = 0.5, en; q = 0.5, en; q = 0.5 "); request-> headers. add ("Accept-encoding", "gzip, deflate, X-gzip, identity; q = 0.9"); // obtain the remote server response Object System: Net :: httpresponse * response = request-> getresponse (); // determines whether an exception occurs if (! Response) {MessageBox (null, request-> getlastexception ()-> message, T ("request error"), 0); goto fail;} If (response-> getlastexception () -> ID! = STATUS_SUCCESS) {MessageBox (null, response-> getlastexception ()-> message, T ("response error"), 0); response-> close (); fail: delete request; return;} // indicates that system: IO: filestream * file = system: IO: file: Create (path, system: IO:: file: createnewmode); If (File) {file-> write (response-> getresponsestream ()-> objectdata, response-> getresponsestream ()-> length ); if (file-> getlastexception ()-> ID! = STATUS_SUCCESS) {MessageBox (null, file-> getlastexception ()-> message, T ("file error"), 0) ;}delete file ;} response-> close (); Delete request; return ;}
Call example:
DownloadFromInternet(T("http://rlib.googlecode.com/files/RLIBTest1.1.zip"), T("C:\\test.zip"));