Best HTTP


http://blog.csdn.net/u012322710/article/details/52860747

 

Best HTTP (Pro)  這是一款很多公司都在用的網頁插件,感覺確實不錯,分Pro版本和普通版本,下載地址:http://www.manew.com/thread-96247-1-1.html

需要你對http短連接有一定的了解。廢話不多說啊,開搞!

 

因為自己找教程的時候,就找到一篇文章,還寫的不多,本來想寫的細一點,把大部分功能都寫一下,還蠻多的,有點偷懶,上傳流文件,下載上傳進度其實插件的PDF都有,看一下就差不多,我這只是拋磚引玉。

 

 

[csharp]  view plain  copy
 
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4. using System.Collections.Generic;  
  5.   
  6. //需要的命名空間  
  7. using BestHTTP;  
  8. using BestHTTP.Statistics;   
  9. using BestHTTP.Cookies;  
  10. using System;  
  11. using System.IO;   
  12.   
  13.   
  14.   
  15. public class bestHttpDemo : MonoBehaviour {  
  16.   
  17.     public RawImage image;  
  18.     public Text showResponse;  
  19.   
  20.   
  21.     //Get請求   不寫HTTPMethods.Get默認也是Get  
  22.     public void OnGetRequest()  
  23.     {  
  24.         HTTPRequest request = new HTTPRequest(new Uri("https://www.baidu.com/"), HTTPMethods.Get, OnRequestFinished);  
  25.         request.Send();  
  26.     }  
  27.   
  28.   
  29.     //請求回調   request請求  response響應  這兩個參數必須要有 委托類型是OnRequestFinishedDelegate  
  30.     void OnRequestFinished(HTTPRequest request, HTTPResponse response)  
  31.     {  
  32.         showResponse.text = "響應:" + response.DataAsText;  
  33.     }  
  34.   
  35.   
  36.     //下載圖片   
  37.     public void OnLoadImage()  
  38.     {  
  39.   
  40.         //Lambda表達式,下載直接回調,簡便寫法。    
  41.         new HTTPRequest(new Uri("http://img.manew.com/data/attachment/forum/201610/19/155755pbw4tt22zznczohh.png"), (request, response) =>  
  42.         {  
  43.   
  44.             image.texture = response.DataAsTexture2D;  
  45.               
  46.             //保存圖片  
  47.             try  
  48.             {  
  49.   
  50.                 if (Application.platform == RuntimePlatform.Android)  
  51.                 {    
  52.                     //在PlayerSetting里修改 WriteAccess寫入入口為外部SDCard   (這里還有問題,安卓里沒存上,還沒搞懂為什么)  
  53.                     //Application.persistentDataPath  在安卓上  /mnt/sdcard/Android/data/com.zou.chongyang/files    
  54.                     File.WriteAllBytes("jar:file://" + Application.persistentDataPath + "/MyImage.png", response.Data);  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     File.WriteAllBytes(Application.dataPath + "/MyImage.png", response.Data);  
  59.                 }  
  60.   
  61.             }  
  62.             catch (IOException e)  
  63.             {  
  64.                 print(e);  
  65.             }    
  66.   
  67.         }).Send();  
  68.   
  69.     }  
  70.       
  71.     /* 
  72.     //最好自己去看BestHTTPDocumentationEN.pdf文檔,功能蠻多的。 
  73.     //BestHttp更多API  還蠻多的,懶得弄到UI上顯示,自己拿着用吧。 
  74.     public void BestHttpAPI() 
  75.     { 
  76.         GeneralStatistics stats = HTTPManager.GetGeneralStatistics(StatisticsQueryFlags.All); //獲取統計信息,統計類型全部 
  77.  
  78.         BestHTTP.Caching.HTTPCacheService.IsSupported        //是否支持緩存(只讀) 
  79.         stats.CacheEntityCount.ToString();                   //緩存對象個數 
  80.         stats.CacheSize.ToString("N0");                      //緩存總大小 
  81.         BestHTTP.Caching.HTTPCacheService.BeginClear();      //清空緩存 
  82.         
  83.         BestHTTP.Cookies.CookieJar.IsSavingSupported        //是否支持保存Cookie(只讀) 
  84.         stats.CookieCount.ToString();                       //Cookie個數 
  85.         stats.CookieJarSize.ToString("N0");                 //Cookie總大小 
  86.         BestHTTP.Cookies.CookieJar.Clear();                 //清空Cookie 
  87.       
  88.         HTTPManager.GetRootCacheFolder()                    //獲取緩存和Cookies目錄路徑 
  89.  
  90.         stats.Connections.ToString();                       //Http連接數 
  91.         stats.ActiveConnections.ToString();                 //激活的Http連接數 
  92.         stats.FreeConnections.ToString();                   //空閑的Http連接數 
  93.         stats.RecycledConnections.ToString();               //回收的Http連接數 
  94.         stats.RequestsInQueue.ToString();                   //Request請求在隊列的數量 
  95.  
  96.         BestHTTP.HTTPManager.OnQuit();                      //退出統計 
  97.       
  98.       
  99.         //緩存維護  緩存最大1mb,   刪除2天前的緩存 
  100.         BestHTTP.Caching.HTTPCacheService.BeginMaintainence(new BestHTTP.Caching.HTTPCacheMaintananceParams( TimeSpan.FromDays(2),1 *1024*1024 )); 
  101.          
  102.         //Cookie維護  刪除7天前的Cookie並保持在最大允許大小內。 
  103.         BestHTTP.Cookies.CookieJar.Maintain(); 
  104.       
  105.         //獲取Cookie集合 
  106.         List<Cookie> cookie = CookieJar.Get(new Uri("https://www.baidu.com/")); 
  107.         //Cookie的API很多 
  108.         cookie[0].Name 
  109.         cookie[0].Domain  
  110.         cookie[0].Value 
  111.     } 
  112.     */  
  113. }  

 

 

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

 

總結:不錯,很好用!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM