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:
DownloadFromInternet(T("http://avatar.csdn.net/9/B/1/1_rrrfff.jpg"), T("C:\\rrrfff.jpg"));
The following is the header file in rlib1.x:
Namespace system {namespace net {// <summary> // obtain the HTTP remote service response information /// </Summary> class export httpresponse {PRIVATE: // response stream responsestream * m_response; httpexception * m_error; private: // initialization member httpresponse () {This-> m_response = NULL; this-> m_error = new httpexception () ;}; public: // get the length of the content returned by the request int contentlength; // get the name of the server sending the response string server; // obtain the response content type string contenttype; // obtain the header responseheader associated with the response from the server Headers; // get the HTTP protocol version string protocolver used in the response; // get the response status // see the http://msdn.microsoft.com/zh-cn/library/system.net.httpstatuscode.aspxint statuscode; // get the status description string statusdescription returned along with the response; // obtain the Internet Resource URI in response to the request // if the request is redirected, the final address lpuri responseuri is returned; public: /// <summary> /// obtain the content of the header returned together with the response /// </Summary> string getResponseHeader (lpcstr headername ); /// <summary> /// gets the stream, which is used to read the body of the response from the server. /// </summ Ary> responsestream * getresponsestream (); // <summary> // close the response stream // </Summary> void close (); /// <summary> /// obtain the exception information generated by httpresponse /// </Summary> httpexception * getlastexception (); public: /// <summary> /// initialize a new instance of the httpresponse Class Based on the specified stream instance // </Summary> static httpresponse * Create (Stream &); /// <summary> /// initialize a new instance of the httpresponse Class Based on the specified stream instance and decompression type /// </Summary> static httpresponse * Create (Stream &, Int automaticdecompression); rlib_classnewdel ;};/// <summary> /// provides access to the HTTP remote server /// </Summary> class export httprequest {PRIVATE: sockets * m_socket; httpexception * m_error; requeststream * m_request; // server response address (after redirection) lpuri m_realuri; stream m_recv_buf; private: bool onconnect (); // connect to the server bool onsend (); // send the request bool onrecv (); // receives the response public: // HTTP headers // specify the set of requestheader headers that constitute the HTTP header name/value pair; // obtain or set the AC Value of the cept http header: String accept; // obtain or set the value of the refererhttp header: String Referer; // obtain or set the value of the connection HTTP header: String connection; // obtain or set the value of the content-typehttp header string contenttype; // obtain or set the value of the User-agenthttp header string useragent; // get or set the Content-Length HTTP header long contentlength; public: // attribute // get or set the decompression type int automaticdecompression used; // obtain or set the request Proxy Information lpproxy requestproxy; // obtain or set the maximum number of redirection requests to be followed // The default value is 3int maximumautomaticre Directions; // gets or sets a value indicating whether the request should follow the redirected response // The default value is truebool autoredirect; // gets or sets whether to ignore the keepalive attribute bool diskeepalive; # pragma region byte completion // bool unused is not used for this attribute; # pragma endregion byte completion // gets or sets a value indicating whether to establish a persistent connection with Internet resources bool keepalive; // obtain the unified Resource Identifier (URI) lpuri address of the request; // obtain or set the Request Method string method; // obtain or set the HTTP Version String protocolver used for the request; // obtain or set the timeout value (in milliseconds). The default value is 8000 (8 seconds) uint timeout. // obtain or set the timeout value (in milliseconds ), the default value is 1000 (1 second ), This attribute is only valid when keepalive = true, // prevents blocking caused by keepalive (the server has completed sending but has not been disconnected) uint recvtimeout; public: // constructor /// <summary> /// initialize a new httprequest instance for the specified URI scheme /// </Summary> httprequest (string ); /// <summary> /// perform necessary cleanup for the httprequest instance /// </Summary> ~ Httprequest (); rlib_classnewdel; public: // <summary> // return the response from Internet resources /// </Summary> httpresponse * getresponse (); /// <summary> /// return the original response stream from Internet resources. The stream contains the response header information /// it should not match getresponse () the method uses /// </Summary> stream * getresponsestream (); /// <summary> /// get the requeststream object used to write request data /// </Summary> requeststream & getrequeststream (); /// <summary> /// reset the request data of httprequest to re-initiate the request. // The post data written earlier will be lost, the Set header will not be lost // </Summary> void resetrequeststream (); /// <summary> /// obtain the exception information generated by httprequest // </Summary> httpexception * getlastexception (); public: /// <summary> /// release all memory resources occupied by httprequest and httpresponse /// this method should be called after all httprequest and httpresponse are no longer used or destructed // /</Summary> static void dispose (); /// <summary> /// return all memory pool memory class pointers shared by httprequest and httpresponse, this method may return NULL // </Summary> static memory * getusingpool ();};}}