Unity- BestHTTP数据请求及文件下载

Unity (BestHTTP)数据请求及文件下载

Unity工程中关于HTTP的使用非常常见,Unity本身集成的WWW便可以进行网络请求处理,但是,WWW本身却有很多的局限性,而BestHttp插件却把HTTP这一块处理的很好,今天学习了一下,并做个简单的笔记,写一个小Demo

创建APIRequestTest对象来管理程序执行过程中关于HTTP的数据请求及下载 这个脚本中有请求完成回调,下载进度回调及下载资源完成回调,基本能满足常规使用,代码注释很清晰

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using BestHTTP;
using UnityEngine;

public class APIRequestTest 
{
    public delegate void HTTPRequestCallBack(APIRequestTest apiRequest,bool isSuccess,string responseText);
    public delegate void HTTPDownProgressCallBack(APIRequestTest apiRequest,float progress);
    public delegate void HTTPDownCompleteCallBack(APIRequestTest apiRequest,bool isSuccess,int code,string savePath);
    private HTTPRequestCallBack requestCallBack;//请求完成回调
    private HTTPDownProgressCallBack downProgressCallBack;//下载进度回调
    private HTTPDownCompleteCallBack downCompleteCallBack;//下载完成回调

    //正式服务器根路径
    private static string baseUrl = "";
    //测试服务器根路径
    private static string testBaseUrl = "http://192.168.2.129:55555/v1/";
    //是否用测试服务器
    public static bool useTestServer = true;

    private HTTPRequest request;
    private TimeSpan requestTimeOut = TimeSpan.FromSeconds(30);//请求超时时间设置 30S

    private string downSavePath;//下载资源的本地保存路径
    private int downFileCount = -1;//需要下载资源的长度
    private int downedFileCount = 0;//已经下载资源的长度
    private float downProgressValue = 0;//下载资源的进度

    private APIRequestTest(string _baseUrl=null) {
		if (!string.IsNullOrEmpty(_baseUrl))
		{
			baseUrl = _baseUrl;
		}
	}

    //创建请求对象
	public static APIRequestTest Create(string _baseUrl=null) {
		if (useTestServer)
		{
			_baseUrl = testBaseUrl;
		}
		return new APIRequestTest(_baseUrl);
	}

    //设置请求超时时间
    public APIRequestTest SetTimeOut(double seconds) {
        if (seconds<=0)
        {
            return this;
        }
        requestTimeOut = TimeSpan.FromSeconds(seconds);
        return this;
    }

    //设置请求优先级  优先级较高的请求将比优先级较低的请求更快地从请求队列中选择。
    public APIRequestTest SetPriority(int priority) {
        if (request!=null)
        {
            request.Priority = priority;
        }
        return this;
    }

    #region 数据请求

    //发送请求
    public APIRequestTest Send(HTTPMethods method,string url,Dictionary<string,string> paramsDict, HTTPRequestCallBack sendCallBack =null) {
        requestCallBack = sendCallBack;
        if (Application.internetReachability==NetworkReachability.NotReachable)//没有网络连接
        {
            Debug.LogError("【 检查设备是否可以访问网络!!! 】");
            requestCallBack?.Invoke(this,false,"设备无法进行网络连接");
            return this;
        }
        if (method ==HTTPMethods.Get)
        {
            SendGetMethod(url,paramsDict);
        }
        else if (method==HTTPMethods.Post)
        {
            SendPostMethod(url,paramsDict);
        }
        if (request==null)
        {
            Debug.LogError("【 创建HTTPRequest失败!!! 】");
            requestCallBack?.Invoke(this, false, "创建HTTPRequest失败");
            return this;
        }
        SetRequestHeaders(request);
        //禁止缓存
        request.DisableCache = true;
        //设置超时时间
        request.Timeout = requestTimeOut;
        //发送请求
        request.Send();
        return this;
    }

    //终止请求
    public APIRequestTest Abort() {
        if (request!=null)
        {
            request.Abort();
        }
        return this;
    }

    //设置请求头
    public void SetRequestHeaders(HTTPRequest _request) {

        //_request.SetHeader("name","value");

    }

    //Get方式请求数据
    void SendGetMethod(string url,Dictionary<string,string> paramsDict) {
        string requestUrl = baseUrl + url+"?";
        if (paramsDict!=null&&paramsDict.Count>0)
        {
            int id = 0;
            foreach (var item in paramsDict.Keys)
            {
                string key = item;
                string value = paramsDict[key];
                if (id < paramsDict.Count - 1)
                {
                    requestUrl += key + "=" + value + "&";
                }
                else {
                    requestUrl += key + "=" + value;
                }
                id++;
            }
        }
        Uri uri = new Uri(requestUrl);
        request = new HTTPRequest(uri,HTTPMethods.Get, OnRequestFinished);
        
    }

    //Post方式请求数据
    void SendPostMethod(string url,Dictionary<string, string> paramsDict) {
        string requestUrl = baseUrl + url;
        Uri uri = new Uri(requestUrl);
        request = new HTTPRequest(uri,HTTPMethods.Post,OnRequestFinished);
        if (paramsDict!=null&&paramsDict.Count>0)
        {
            foreach (var item in paramsDict.Keys)
            {
                string key = item;
                string value = paramsDict[key];
                request.AddField(key,value);
            }
        }
        
    }

    //数据请求完成回调
    void OnRequestFinished(HTTPRequest _request, HTTPResponse response) {
        int responseCode = 0;//请求返回码
        bool isSuccess = false;
        string responseText = null;

        if (_request.State == HTTPRequestStates.Finished)//请求完成
        {
            responseCode = response.StatusCode;
            isSuccess = response.IsSuccess;
            responseText = response.DataAsText;
        }
        else if (_request.State == HTTPRequestStates.TimedOut || _request.State == HTTPRequestStates.ConnectionTimedOut)//请求或连接超时
        {
            responseText = "请求超时";
        }
        else if (_request.State == HTTPRequestStates.Error)//请求报错
        {
            if (request.Exception != null)
            {
                responseText = request.Exception.Message;
                if (string.IsNullOrEmpty(responseText))
                {
                    responseText = "请求报错";
                }
            }
            else
            {
                responseText = "请求报错";
            }
        }
        else {//其他异常
            responseText = _request.State.ToString();
        }
#if UNITY_EDITOR  //编辑器模式下打印请求数据
        string debugStr = "【HTTPRequest  State="+ _request.State.ToString()+"  isSuccess="+isSuccess+"】:";
        string url = _request.Uri.AbsoluteUri+"\n";
        debugStr = "<color=blue>"+debugStr + url+"</color>" + System.Text.RegularExpressions.Regex.Unescape(responseText);
        Debug.Log(debugStr);
#endif

        requestCallBack?.Invoke(this,isSuccess,responseText);

    }

    #endregion 


    #region 资源下载

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="url">资源url</param>
    /// <param name="savePath">保存本地的路径</param>
    /// <param name="progressCallBack">下载进度回调</param>
    /// <param name="completeCallBack">下载完成回调</param>
    /// <param name="forceDown">是否强制下载(是否替换本地文件)</param>
    public void DownFile(string url, string savePath, HTTPDownProgressCallBack progressCallBack, HTTPDownCompleteCallBack completeCallBack, bool forceDown = false) {
        downCompleteCallBack = completeCallBack;
        downProgressCallBack = progressCallBack;
        downSavePath = savePath;
        downFileCount = -1;
        downedFileCount = 0;
        if (string.IsNullOrEmpty(url)||string.IsNullOrEmpty(savePath))
        {
            Debug.LogError("【 非法的下载地址或存储地址! 】");
            downCompleteCallBack?.Invoke(this,false,300,savePath);
            return;
        }

        if (!forceDown)//非强制下载
        {
            if (File.Exists(savePath))//本地存在目标文件
            {
                if (savePath.EndsWith(".tmp", StringComparison.CurrentCulture))
                {//删除本地临时文件
                    File.Delete(savePath);
                    Debug.Log("删除文件:"+savePath);
                }
                else {
                    Debug.Log("重复资源,无需下载:"+savePath);
                    downProgressCallBack?.Invoke(this,1);
                    downCompleteCallBack?.Invoke(this, true, 200, savePath);
                    return;
                }
            }
            else {//新资源
                Debug.Log("新资源:"+savePath);
            }
        }

        Uri uri = new Uri(url);
        request = new HTTPRequest(uri,DownRequestFinished);
        request.UseStreaming = true;
        request.StreamFragmentSize = 1024 * 512;
        request.DisableCache = true;
        request.Timeout = requestTimeOut;
        request.ConnectTimeout =requestTimeOut;
        request.Send();
    }

    //下载资源请求结束回调
    private void DownRequestFinished(HTTPRequest _request, HTTPResponse response) {

        HTTPRequestStates states = _request.State;
        if (states==HTTPRequestStates.Processing)//数据请求中
        {
            if (downFileCount == -1)
            {
                string value = response.GetFirstHeaderValue("content-length");
                if (!string.IsNullOrEmpty(value))
                {
                    downFileCount= int.Parse(value);
                    Debug.Log("GetFirstHeaderValue content-length = " + downFileCount);
                }
            }
            List<byte[]> bytes;
            bytes = response.GetStreamedFragments();
            SaveFile(bytes);
            downProgressValue = downedFileCount / (float)downFileCount;
            downProgressCallBack?.Invoke(this,downProgressValue);
        }
        else
        if (states == HTTPRequestStates.Finished)//数据请求完成
        {
            if (response.IsSuccess)
            {
                List<byte[]> bytes;
                bytes = response.GetStreamedFragments();
                SaveFile(bytes);
                if (response.IsStreamingFinished)
                {
                    downProgressCallBack?.Invoke(this, 1);
                    downCompleteCallBack?.Invoke(this,true,response.StatusCode,downSavePath);
                }
                else {
                    Debug.LogError("【HTTPRequestStates.Finished成功,Response.IsStreamingFinished失败】");
                    downCompleteCallBack?.Invoke(this, false, response.StatusCode, downSavePath);
                }
            }
            else {

                downCompleteCallBack?.Invoke(this, false, response.StatusCode, downSavePath);
                string logMsg = string.Format("{0} - {1} - {2}", response.StatusCode, response.Message, _request.Uri.AbsoluteUri);
                Debug.LogError(logMsg);
            }
        }
        else {//数据请求失败
            Debug.LogError("DownFile失败:"+_request.State.ToString());
            downCompleteCallBack?.Invoke(this,false,-1, downSavePath);
        }

    }

    //保存资源到本地
    private void SaveFile(List<byte[]> bytes) {
        if (bytes==null||bytes.Count<=0)
        {
            return;
        }

        using (FileStream fs = new FileStream(downSavePath,FileMode.Append)) {
            for (int i = 0; i < bytes.Count; i++)
            {
                byte[] byteArr = bytes[i];
                fs.Write(byteArr,0,byteArr.Length);
                downedFileCount+=byteArr.Length;
            }
        }

    }

    #endregion 


}

使用

请求数据

string url = "student/report_card";
Dictionary<string, string> info = new Dictionary<string, string>();
info["lesson_id"] = "1287";
APIRequestTest.Create().Send(BestHTTP.HTTPMethods.Get, url, a, (APIRequestTest api, bool isSuccess, string responseText) =>{
     if (isSuccess)
       {
           Debug.LogError("请求成功");
       }
       else
       {
           Debug.LogError("请求失败");
       }
       Debug.LogError(responseText);
   });

资源下载

string downUrl = "资源网络路径";
string savePath = Application.streamingAssetsPath + "/testVideo.mp4";

APIRequestTest.Create().DownFile(downUrl, savePath, (APIRequestTest apiRequest, float value) =>
  {
      Debug.LogError(value.ToString("f2"));
  }, (APIRequestTest api, bool isSuccess, int code, string path) =>
   {
       if (isSuccess)
       {
           Debug.LogError("下载成功");
       }
       else
       {
           Debug.LogError("下载失败");
       }
       Debug.LogError(path);
   });


Unity中有许多不同的HTTP库可以用来下载资源和与网络服务器交互。其中最流行的HTTP库之一是UnityWebRequest。UnityWebRequest是一个内置的类,可以方便地发送HTTP请求、接收响应并处理网络数据。它提供了对GET、POST和其他HTTP方法的支持,还可以通过设置请求头和表单来发送和接收数据UnityWebRequest还支持断点续传和下载进度监控等功能,非常适合用来下载资源和进行网络通信。另外,由于UnityWebRequest是Unity内置的类,它对于与Unity引擎的整合和兼容性都很好,可以方便地和其他Unity组件一起使用。 除了UnityWebRequest之外,还有一些第三方的HTTP库也可以用来进行网络通信,比如BestHTTP、ProHTTP等。这些HTTP库通常提供了更多的功能和更高级的特性,比如多线程下载、SSL支持、WebSocket通信等。其中,BestHTTP被认为是最优秀的HTTP库之一,它不仅提供了丰富的功能和易用的接口,还有良好的性能和稳定性。BestHTTP也能很好地和Unity引擎进行整合,可以方便地用来下载资源、进行网络通信和与服务器进行交互。 综上所述,Unity中有许多HTTP库可以用来进行网络通信和下载资源,其中UnityWebRequest是内置的HTTP库,适合简单的网络请求和资源下载;而BestHTTP则是一个优秀的第三方HTTP库,可以提供更多的功能和更高级的特性,适合复杂的网络通信和服务器交互。在选择HTTP库时,可以根据项目的需求和实际情况来选择合适的库来进行使用。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司军礼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值