unity BestHttpV3 http请求发送

BestHttpV3 实例代码

The world is changing, and I’m just trying to keep up. ----Arthur Morgan

实现功能:

  1. 发送请求
  2. 下载图片保存_阻塞式
  3. 下载图片保存——轮询式
  4. 下载小文件(图片),不保存
  5. 发送表单文件

插件导入

UniTask

https://github.com/Cysharp/UniTask

BestHttp v3,这个插件有v1,v2,v3版本,每个版本的文档都不相同!

https://assetstore.unity.com/packages/tools/network/best-http-267636

在这里插入图片描述

参考文档

//快速开始
//https://bestdocshub.pages.dev/HTTP/getting-started/#1-create-request

//BestHttp 总文档
//https://bestdocshub.pages.dev/

//测试网站
//httpbin.org

//文件下载部分的文档
//https://bestdocshub.pages.dev/HTTP/getting-started/downloads/#progress-tracking

//BestHttp v1的文档(过时)
//https://besthttp-documentation.readthedocs.io/en/dev/1.HTTP/HTTPRequest/

最佳实践代码

using Best.HTTP;
using Best.HTTP.Request.Upload.Forms;
using Best.HTTP.Response;
using Best.HTTP.Shared.PlatformSupport.Memory;
using Cysharp.Threading.Tasks;
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

public class BestHttp_ECode : MonoBehaviour
{
    string tmp_postURL = "http://www.baidu.com";

    private string path_bigFilePatg = "http://n.sinaimg.cn/sinacn/w480h306/20180118/6452-fyqtwzu4786357.jpg";

    private string path_imageURL = "http://n.sinaimg.cn/sinacn/w480h306/20180118/6452-fyqtwzu4786357.jpg";

    void Start()
    {
        StartCoroutine(nameof(Test_Get));
        StartCoroutine(nameof(ShowPictureByHttpRequest));
        StartCoroutine(nameof(Test_SendPost));


        StartCoroutine(nameof(DownBigFile_WithBlicking));
        StartCoroutine(nameof(Test_DownLoadABigFile_WithPolling));
    }


    private IEnumerator Test_Get()
    {
        HTTPRequest httpRequest = new HTTPRequest(new System.Uri(tmp_postURL), HTTPMethods.Get
           , (request, response) =>
           {
               if (response.IsSuccess)
               {
                   Debug.Log(response.DataAsText);
               }
               else
               {
                   Debug.Log("发送失败");
               }
           });
        httpRequest.Send();
        yield return null;
    }


    /// <summary>
    ///  Post测试方法
    /// </summary>
    private IEnumerator Test_SendPost()
    {
        HTTPRequest tmp_post = new HTTPRequest(new System.Uri(tmp_postURL), HTTPMethods.Post,
            (request, response) => { Debug.Log(response.DataAsText); });

        yield return tmp_post.Send();
    }

    private IEnumerator AddFormData()
    {
        HTTPRequest tmp_post = new HTTPRequest(new System.Uri(tmp_postURL), HTTP
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值