Unity3d 網頁插件BestHttp使用介紹

原創


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

總結:不錯,很好用!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章