Unity-Http协议请求下载系列4:Http应用测试

40 篇文章 3 订阅
本文介绍了如何在Unity中使用HttpWebRequest和UnityWebRequest两种方式实现HTTP协议请求下载资源,包括完全下载和断点续传。通过测试,展示了两种接口的使用方法和测试结果,为游戏开发中的资源更新提供了参考。
摘要由CSDN通过智能技术生成

上篇文章介绍完了Http协议种请求下载资源的方式,现在需要测试。关于本地搭建服务器,读者可以查看本人写的另外一篇博客Tomcat服务器

测试类HttpTest

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

/*
 * Author:W
 * http协议请求下载测试
 */
namespace W.GameFramework.HotUpdate
{
	public class HttpTest : MonoBehaviour
	{
		private string url = "http://localhost:8080/download/nguichajian.zip";
		//C:/Users/lunqiang.wang/Desktop/UnityHot2/UnityHotFix-master/Assets
		private string rootPath;
		private string filePath;
		private HttpHelper httpHelper = null;

		public Button newDownloadBtn;
		public Button continueDownloadBtn;

		public Button newDownloadBtn2;
		public Button continueDownloadBtn2;

		// Use this for initialization
		void Start()
		{
			newDownloadBtn.onClick.AddListener(OnNewDownloadBtnClick);
			continueDownloadBtn.onClick.AddListener(OnContinueDownloadBtnClick);

			newDownloadBtn2.onClick.AddListener(OnNewDownloadBtnClick2);
			continueDownloadBtn2.onClick.AddListener(OnContinueDownloadBtnClick2);
			
			rootPath = Application.dataPath.Replace("Assets", "") + "download";
			if (!Directory.Exists(rootPath))
				Directory.CreateDirectory(rootPath);

			filePath = rootPath + "/nguichajian.zip";						
        }

		void Update()
		{
			if (Input.GetKeyDown(KeyCode.P))
			{
				//模拟中途暂停下载
				if(httpHelper!=null)
				   httpHelper.IsStop = true;
			}
		}

		private void OnNewDownloadBtnClick()
		{
			Debug.Log("=======HttpWebRequest 完全下载============");
			httpHelper = new HttpWebRequestHelper();
			httpHelper.SendHttpRequest(url, filePath, DownloadType.New, OnDownLoadBeginHandler, OnDownloadEndHandler,
				OnDownloadProgressHandler, OnDownloadErrorHandler);
		}

		private void OnContinueDownloadBtnClick()
		{
			Debug.Log("=======HttpWebRequest 断点续传下载============");

			httpHelper = new HttpWebRequestHelper();
			httpHelper.SendHttpRequest(url, filePath, DownloadType.Continue, OnDownLoadBeginHandler, OnDownloadEndHandler,
				OnDownloadProgressHandler, OnDownloadErrorHandler);
		}


		private void OnNewDownloadBtnClick2()
		{
			Debug.Log("=======UnityWebRequest 完全下载============");

			httpHelper = new UnityWebHttpRequestHelper();
			httpHelper.SendHttpRequest(url, filePath, DownloadType.New, OnDownLoadBeginHandler, OnDownloadEndHandler,
				OnDownloadProgressHandler, OnDownloadErrorHandler);
		}

		private void OnContinueDownloadBtnClick2()
		{
			Debug.Log("=======UnityWebRequest 断点续传下载============");

			httpHelper = new UnityWebHttpRequestHelper();
			httpHelper.SendHttpRequest(url, filePath, DownloadType.Continue, OnDownLoadBeginHandler, OnDownloadEndHandler,
				OnDownloadProgressHandler, OnDownloadErrorHandler);
		}




		private void OnDownLoadBeginHandler()
		{
			Debug.Log("资源:"+httpHelper.url+" 开始下载!");
		}

		private void OnDownloadEndHandler()
		{
		   Debug.Log("资源:" + httpHelper.url+" 下载完成,保存路径:"+httpHelper.path);
		}

		private void OnDownloadProgressHandler(float progress)
		{
			Debug.Log("资源:" + httpHelper.url+" 当前进度==="+progress);
		}

		private void OnDownloadErrorHandler(string errorCode)
		{
			Debug.LogError("资源:"+url+" 下载错误error="+errorCode);
		}

	
	}
}

测试UI如下

模拟测试结果如下

HttpWebRequest接口测试结果

UnityWebRequest接口测试结果

 至此关于Unity实现Http协议请求加载2种实现方式已经完成!供大家交流借鉴。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果你需要在 Unity WebGL 中从服务器接收这些数据并进行处理,可以使用以下代码: ```csharp using System; using UnityEngine.Networking; public class Example : MonoBehaviour { public string url; // 服务器地址 public string starttime = "2023-05-07 09:54:22"; public string endtime = "2023-06-07 09:54:22"; IEnumerator Start() { // 创建一个 UnityWebRequest 对象 UnityWebRequest request = UnityWebRequest.Get(url); // 发送请求并等待服务器响应 yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) { // 请求失败,输出错误信息 Debug.Log(request.error); } else { // 请求成功,从响应数据中获取 starttime 和 endtime string responseText = request.downloadHandler.text; // 假设响应数据为 JSON 格式,可以使用 JsonUtility 来解析 ResponseData responseData = JsonUtility.FromJson<ResponseData>(responseText); starttime = responseData.starttime; endtime = responseData.endtime; // 将字符串类型的 starttime 和 endtime 转换为 DateTime 类型 DateTime startTime = DateTime.ParseExact(starttime, "yyyy-MM-dd HH:mm:ss", null); DateTime endTime = DateTime.ParseExact(endtime, "yyyy-MM-dd HH:mm:ss", null); // 计算时间差 TimeSpan duration = endTime - startTime; // 输出时间差 Debug.Log(duration); } } [Serializable] private class ResponseData { public string starttime; public string endtime; } } ``` 在这个示例中,我们使用 UnityWebRequest 来向服务器发送请求,并等待服务器的响应。如果请求失败,我们将会输出错误信息;如果请求成功,我们将会从响应数据中获取 starttime 和 endtime,并计算它们之间的时间差。需要注意的是,在这个示例中,我们假设响应数据为 JSON 格式,可以使用 `JsonUtility` 来解析。如果响应数据不是 JSON 格式,你需要使用其他方法来解析响应数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Data菌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值