码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d C# HttpWebRequest 异步下载文件

时间:2015-03-12 15:07:34      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:unity   il2cpp ios64   httpwebrequest   异步   

最近一直在把公司游戏迁移到支持ARM64,中间出现了很多BUG或者Unity目前不支持操作的问题,这两天遇到异步HttpWebRequest不支持的问题,因为之前一直没有接触,所以也趁机学习了下相关使用。


下面通过一个例子,来使用HttpWebRequest异步下载一个ZIP文件。


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 initialization
	void 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");
            //异步获取;
            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;

            //开始读取数据;
            IAsyncResult asyncreadresult = responseStream.BeginRead(myRequestState.m_bufferRead, 0, 1024, 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)
            {
                //将接收的数据写入;
                fileStream.Write(myRequestState.m_bufferRead, 0, 1024);
                fileStream.Flush();
                //fileStream.Close();

                //继续读取数据;
                myRequestState.m_bufferRead = new byte[1024];
                IAsyncResult asyncreadresult = responseStream.BeginRead(myRequestState.m_bufferRead, 0, 1024, new AsyncCallback(ReadCallBack), myRequestState);
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.ToString());
        }
    }


    void TimeoutCallback(object state, bool timeout)
    {
        if (timeout)
        {
            HttpWebRequest request = state as HttpWebRequest;
            if (request!=null)
            {
                request.Abort();
            }
            
        }
    }
	

	// Update is called once per frame
	void Update () {
	
	}
}


Unity3d C# HttpWebRequest 异步下载文件

标签:unity   il2cpp ios64   httpwebrequest   异步   

原文地址:http://blog.csdn.net/huutu/article/details/44221469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!