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

总结:不错,很好用!

原文地址:https://www.cnblogs.com/sanyejun/p/7911677.html