当前位置: 首页 > 工具软件 > httphelper > 使用案例 >

Unity-Http协议请求下载系列1:接口封装HttpHelper

柳业
2023-12-01

游戏请求服务器下载资源,是很常见的操作。特别是有热更的资源游戏,游戏启动会检查下载Bundle资源。使用的是Http协议。本人查看很多资料,实现服务器请求下载资源的方式大概有2种,其一是NET框架提供的HttpWebRequest接口,另外就是Unit引擎提供的UnityWebRequest接口。另外,是否支持断点续传。本人整合封装了上述2中实现方式,并且支持完全重新下载和断点续传2种下载模式。

    这篇文章主要介绍实现Http请求的接口封装类:HttpHelper

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * Web请求工具
 */
namespace W.GameFramework.HotUpdate
{
	/// <summary>
	/// 下载方式:重新或者断点续传
	/// </summary>
	public enum DownloadType
	{
		New,//重新下载
	    Continue,//断点续传下载
	}
	/// <summary>
	/// 开始下载回调
	/// </summary>
	public delegate void OnDownloadBeginHandler();
	/// <summary>
	/// 下载进度回调
	/// </summary>
	/// <param name="url"></param>
	/// <param name="progress"></param>
	public delegate void OnDownloadProgressHanlder(float progress);
	/// <summary>
	/// 下载结束回调
	/// </summary>
	public delegate void OnDownloadEndHandler();
	/// <summary>
	/// 下载错误
	/// </summary>
	public delegate void OnDownloadErrorHandler(string errorCode);

	public  class HttpHelper
	{
		/// <summary>
		/// 当前下载资源资源URL地址
		/// </summary>
		public  string url;
		/// <summary>
		/// 当前下载资源的保存路径
		/// </summary>
		public string path;


		private float _progress = 0f;
		/// <summary>
		/// 当前下载的进度
		/// </summary>
		protected float progress
		{
			get { return _progress; }

			set 
			{
				_progress = value;

				if (OnDownloadProgressHanlder != null)
					OnDownloadProgressHanlder(value);
			}
		}

		/// <summary>
		/// 文件当前已下载的长度
		/// </summary>
		protected long fileCurLength = 0;
		/// <summary>
		/// 当前下载文件的总长度
		/// </summary>
		protected long fileLength = 0;


		/// <summary>
		/// 请求时间
		/// </summary>
		protected int timeOut = 100000;
		/// <summary>
		/// 请求方式
		/// </summary>
		protected string method = "GET";

		private bool _isStop = false;
		/// <summary>
		/// 是否暂停
		/// </summary>
		public bool IsStop
		{
			get { return this._isStop; }

			set { this._isStop = value; }
		}

		/// <summary>
		/// 请求下载方式
		/// </summary>
		protected DownloadType downloadType = DownloadType.New;

		/// <summary>
		/// 下载开始回调
		/// </summary>
		protected OnDownloadBeginHandler OnDownloadBeginHandler;
		/// <summary>
		/// 下载结束回调
		/// </summary>
		protected OnDownloadEndHandler OnDownloadEndHandler;

		/// <summary>
		/// 下载进度回调
		/// </summary>
		protected OnDownloadProgressHanlder OnDownloadProgressHanlder;

		/// <summary>
		/// 下载错误异常回调
		/// </summary>
		protected OnDownloadErrorHandler OnDownloadErrorHandler;



		/// <summary>
		/// 向服务器发出请求下载url资源
		/// </summary>
		/// <param name="url">资源网络地址</param>
		/// <param name="path">资源保存路径</param>
		/// <param name="downloadType">下载方式:重新下载或断点续传</param>
		/// <param name="onDownloadBeginHandler">下载开始回调</param>
		/// <param name="onDownloadEndHandler">下载完成回调</param>
		/// <param name="onDownloadErrorHandler">下载错误回调</param>
		public virtual void SendHttpRequest(string url, string path,DownloadType downloadType = DownloadType.New, OnDownloadBeginHandler onDownloadBeginHandler = null, OnDownloadEndHandler onDownloadEndHandler = null,
			OnDownloadProgressHanlder onDownloadProgressHanlder = null,OnDownloadErrorHandler onDownloadErrorHandler = null)
		{
			this.url = url;
			this.path = path;
			this.downloadType = downloadType;
			this.OnDownloadBeginHandler = onDownloadBeginHandler;
			this.OnDownloadEndHandler = onDownloadEndHandler;
			this.OnDownloadProgressHanlder = onDownloadProgressHanlder;
			this.OnDownloadErrorHandler = onDownloadErrorHandler;
		}

		/// <summary>
		/// 清理
		/// </summary>
		public virtual void Clear() { }
	}
}

下篇文章链接https://blog.csdn.net/wlqchengzhangji/article/details/118704311

 类似资料: