C ++ Download Simple HTTP resource code

Source: Internet
Author: User
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"));

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.