Unity Download File III (HTTP asynchronous download)

Source: Internet
Author: User

Asynchronous download, as the name implies, does not affect your main thread when using the client, people in the background to engage in your Ming Tang.

directly into the topic, since to download, the first request, the request after the successful callback, this is an asynchronous process, asynchronous callback time is not controllable.

1, the first request to download.

    Public  bool DownLoadFile (downloadfilemodel file)    {        try        {            HttpWebRequest request = ( HttpWebRequest) webrequest.create ("http://192.168.1.1:8080/" + file. FileName);
       Downloadfilemodel an entity class defined for me, the file name needs a suffix name
File.request = request;//The request information for HTTP is also taken in the entity class to facilitate the judgment in the callback
Request. BeginGetResponse (New AsyncCallback (responsecallbackdownload), file); Responsecallbackdownload callback method After successful request return true; Catch { return false; } }

2, the request succeeds after will call the Responsecallbackdownload method, this method is asynchronous, is executes in the sub-thread, if in the message person mode in this callback inside sends hears the main thread, then the operation Gameobject object will error, or directly call the main thread of the Gameobject will also error, as to how to get this download information and operation interface, I think I do not need to say more. Not much to say, on the callback code, the callback code is executed in the child thread.

 private void Responsecallbackdownload (IAsyncResult ar) {//throw new notimplementedexception (); try {object req = ar.            AsyncState as Object;;            if (req = = null) return; Downloadfilemodel file = ar. AsyncState as downloadfilemodel;//gets information about the request for an asynchronous download in order to determine httpwebresponse response = File.request.EndGetResponse (AR            ) as HttpWebResponse; if (response. StatusCode! = Httpstatuscode.ok) {Response.                Close ();            Return            } Stream OutStream; Stream instream = Response.                        GetResponseStream ();            Byte[] B = new byte[1024]; FileInfo fi = new FileInfo (application.persistentdatapath+ "//" + file.)            Savename); if (FI.                Exists) {//todo If the file exists, TODO} else//else If the file does not exist { try {int readcount = Instream.read (b, 0, b.length);                    OutStream = fi.                    Create (); Long filelength = Response.                    contentlength;//get the total file length long fileSize = 0;//downloaded file length while (Readcount > 0)                        {Outstream.write (b, 0, readcount);                        FileSize + = Readcount; int progress = (int) ((float) fileSize/(float) filelength);//progress to calculate the percentage of downloads readcount =                    Instream.read (b, 0, b.length);                    } outstream.close ();                    Instream.close (); Response.                    Close ();         }} catch (Exception ex) {}}} catch (Exception ex) {Debug.Log ("File download Failed" + ex.)        Message);     }    }}

  

Unity Download File III (HTTP asynchronous download)

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.