Unity3d 网页插件BestHttp使用介绍

本文介绍了BestHTTP(Pro)插件的基本使用方法,包括GET请求、图片下载及保存等功能,并提供了部分API示例,适合希望快速掌握此插件的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创


Best HTTP (Pro)  这是一款很多公司都在用的网页插件,感觉确实不错,分Pro版本和普通版本,下载地址:http://www.manew.com/thread-96247-1-1.html

需要你对http短连接有一定的了解。废话不多说啊,开搞!


因为自己找教程的时候,就找到一篇文章,还写的不多,本来想写的细一点,把大部分功能都写一下,还蛮多的,有点偷懒,上传流文件,下载上传进度其实插件的PDF都有,看一下就差不多,我这只是抛砖引玉。


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

//需要的命名空间
using BestHTTP;
using BestHTTP.Statistics; 
using BestHTTP.Cookies;
using System;
using System.IO; 



public class bestHttpDemo : MonoBehaviour {

    public RawImage image;
    public Text showResponse;


    //Get请求   不写HTTPMethods.Get默认也是Get
    public void OnGetRequest()
    {
        HTTPRequest request = new HTTPRequest(new Uri("https://www.baidu.com/"), HTTPMethods.Get, OnRequestFinished);
        request.Send();
    }


    //请求回调   request请求  response响应  这两个参数必须要有 委托类型是OnRequestFinishedDelegate
    void OnRequestFinished(HTTPRequest request, HTTPResponse response)
    {
        showResponse.text = "响应:" + response.DataAsText;
    }


    //下载图片 
    public void OnLoadImage()
    {

        //Lambda表达式,下载直接回调,简便写法。  
        new HTTPRequest(new Uri("http://img.manew.com/data/attachment/forum/201610/19/155755pbw4tt22zznczohh.png"), (request, response) =>
        {

            image.texture = response.DataAsTexture2D;
            
            //保存图片
            try
            {

                if (Application.platform == RuntimePlatform.Android)
                {  
                    //在PlayerSetting里修改 WriteAccess写入入口为外部SDCard   (这里还有问题,安卓里没存上,还没搞懂为什么)
                    //Application.persistentDataPath  在安卓上  /mnt/sdcard/Android/data/com.zou.chongyang/files  
                    File.WriteAllBytes("jar:file://" + Application.persistentDataPath + "/MyImage.png", response.Data);
                }
                else
                {
                    File.WriteAllBytes(Application.dataPath + "/MyImage.png", response.Data);
                }

            }
            catch (IOException e)
            {
                print(e);
            }  

        }).Send();

    }
    
    /*
    //最好自己去看BestHTTPDocumentationEN.pdf文档,功能蛮多的。
    //BestHttp更多API  还蛮多的,懒得弄到UI上显示,自己拿着用吧。
    public void BestHttpAPI()
    {
        GeneralStatistics stats = HTTPManager.GetGeneralStatistics(StatisticsQueryFlags.All); //获取统计信息,统计类型全部

        BestHTTP.Caching.HTTPCacheService.IsSupported        //是否支持缓存(只读)
        stats.CacheEntityCount.ToString();                   //缓存对象个数
        stats.CacheSize.ToString("N0");                      //缓存总大小
        BestHTTP.Caching.HTTPCacheService.BeginClear();      //清空缓存
       
        BestHTTP.Cookies.CookieJar.IsSavingSupported        //是否支持保存Cookie(只读)
        stats.CookieCount.ToString();                       //Cookie个数
        stats.CookieJarSize.ToString("N0");                 //Cookie总大小
        BestHTTP.Cookies.CookieJar.Clear();                 //清空Cookie
     
        HTTPManager.GetRootCacheFolder()                    //获取缓存和Cookies目录路径

        stats.Connections.ToString();                       //Http连接数
        stats.ActiveConnections.ToString();                 //激活的Http连接数
        stats.FreeConnections.ToString();                   //空闲的Http连接数
        stats.RecycledConnections.ToString();               //回收的Http连接数
        stats.RequestsInQueue.ToString();                   //Request请求在队列的数量

        BestHTTP.HTTPManager.OnQuit();                      //退出统计
     
     
        //缓存维护  缓存最大1mb,   删除2天前的缓存
        BestHTTP.Caching.HTTPCacheService.BeginMaintainence(new BestHTTP.Caching.HTTPCacheMaintananceParams( TimeSpan.FromDays(2),1 *1024*1024 ));
        
        //Cookie维护  删除7天前的Cookie并保持在最大允许大小内。
        BestHTTP.Cookies.CookieJar.Maintain();
     
        //获取Cookie集合
        List<Cookie> cookie = CookieJar.Get(new Uri("https://www.baidu.com/"));
        //Cookie的API很多
        cookie[0].Name
        cookie[0].Domain 
        cookie[0].Value
    }
    */
}


Cookie介绍: https://my.oschina.net/jihan19921016/blog/506473

总结:不错,很好用!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我是重阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值