Unity3d C # httpwebrequest asynchronous download file

Source: Internet
Author: User

Recently has been the company's game to support ARM64, there are a lot of bugs or unity currently does not support the operation of the problem, these two days encountered asynchronous HttpWebRequest not support the problem, because there has been no contact, so also took the opportunity to learn about the use of the next.


Here's an example of using HttpWebRequest to download a zip file asynchronously.


Using unityengine;using system.collections;using system.net;using system.io;using system;using System.Threading;    Using System.text;public class requeststate{const int m_buffetsize = 1024;    Public StringBuilder M_requestdata;    Public byte[] M_bufferread;    Public HttpWebRequest M_request;    Public HttpWebResponse M_response;    Public Stream M_streamresponse;        Public RequestState () {m_bufferread = new byte[m_buffetsize];        M_requestdata = new StringBuilder ("");        M_request = null;    M_streamresponse = null;  }}public class Newbehaviourscript:monobehaviour {FileStream filestream=null;//Use this for initializationvoid Start        () {fileStream = new FileStream ("Disunity_v0.3.4.zip", FileMode.Create);    Downloadmusicasyn ();}        void Downloadmusicasyn () {Debug.Log ("Downloadmusicasyn Thread Start"); try {HttpWebRequest myhttpwebrequest = (HttpWebRequest) webrequest.create ("http://dev.thisisgame.com.Cn/disunity_v0.3.4.zip ");            RequestState myrequeststate = new RequestState ();            Myrequeststate.m_request = myHttpWebRequest;            Debug.Log ("BeginGetResponse Start");            asynchronous acquisition; IAsyncResult result = (IAsyncResult) myhttpwebrequest.begingetresponse (new AsyncCallback (Respcallback),            Myrequeststate);        Debug.Log ("BeginGetResponse End"); } catch (System.Exception ex) {Debug.logerror (ex.        ToString ());        }} void Respcallback (IAsyncResult result) {Debug.Log ("Respcallback 0"); try {requeststate myrequeststate = (requeststate) result.            asyncstate;            HttpWebRequest myhttpwebrequest = myrequeststate.m_request;            Debug.Log ("Respcallback endgetresponse");            Myrequeststate.m_response = (HttpWebResponse) myhttpwebrequest.endgetresponse (result); Stream Responsestream = Myrequeststate.m_response.            GetResponseStream (); MyrequeststatE.m_streamresponse = Responsestream;            Start reading data; IAsyncResult Asyncreadresult = responsestream.beginread (myrequeststate.m_bufferread, 0, 1024x768, new AsyncCallback (            Readcallback), myrequeststate);        Return } catch (System.Exception ex) {Debug.logerror (ex.        ToString ());        }} void Readcallback (IAsyncResult result) {Debug.Log ("readcallback"); try {requeststate myrequeststate = (requeststate) result.            asyncstate;            Stream responsestream = myrequeststate.m_streamresponse;            int read = Responsestream.endread (result);            Debug.Log ("Read size =" + read);                if (Read > 0) {//writes the received data;                FileStream.Write (myrequeststate.m_bufferread, 0, 1024);                Filestream.flush ();                Filestream.close ();                continue to read data;                Myrequeststate.m_bufferread = new byte[1024]; IasYncresult Asyncreadresult = responsestream.beginread (myrequeststate.m_bufferread, 0, 1024x768, new AsyncCallback (            Readcallback), myrequeststate); }} catch (System.Exception ex) {Debug.logerror (ex).        ToString ()); }} void Timeoutcallback (object state, bool timeout) {if (timeout) {HttpWebRequest req            Uest = state as HttpWebRequest; if (request!=null) {request.            Abort (); }}}//update is called once per framevoid update () {}}


Unity3d C # httpwebrequest asynchronous download file

Related Article

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.