WebRequest.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace EZXR.Glass.Network.WebRequest
  5. {
  6. /// <summary>
  7. /// 得到服务器返回的数据后返回给调用方,调用方如需要得到返回的数据必须实现此delegate,identifier供用户分辨出这个返回数据是谁的
  8. /// </summary>
  9. public delegate void GiveBackString(string data, string identifier, long statusCode = 200);
  10. /// <summary>
  11. /// 得到服务器返回的数据后返回给调用方,调用方如需要得到返回的数据必须实现此delegate,identifier供用户分辨出这个返回数据是谁的
  12. /// </summary>
  13. public delegate void GiveBackStringArray(string[] data, string[] identifier, long statusCode = 200);
  14. /// <summary>
  15. /// 得到服务器返回的数据后返回给调用方,调用方如需要得到返回的数据必须实现此delegate,identifier供用户分辨出这个返回数据是谁的
  16. /// </summary>
  17. public delegate void GiveBackBytes(byte[] data, string identifier, long statusCode = 200);
  18. /// <summary>
  19. /// 得到服务器返回的数据后返回给调用方,调用方如需要得到返回的数据必须实现此delegate,identifier供用户分辨出这个返回数据是谁的
  20. /// </summary>
  21. public delegate void GiveBackBytesArray(byte[][] data, string[] identifier, long[] statusCode);
  22. /// <summary>
  23. /// 返回当前请求的加载进度
  24. /// </summary>
  25. public delegate void GiveBackLoadingProgress(UnityEngine.Networking.UnityWebRequest unityWebRequest, string identifier);
  26. public enum RequestMode
  27. {
  28. GET,
  29. POST
  30. }
  31. public partial class UnityWebRequest : MonoBehaviour
  32. {
  33. #region 单例、构造
  34. private static UnityWebRequest _instance = null;
  35. private static readonly object SynObject = new object();
  36. public static UnityWebRequest Instance
  37. {
  38. get
  39. {
  40. lock (SynObject)
  41. {
  42. if (_instance == null)
  43. {
  44. GameObject obj = new GameObject("UnityWebRequest");
  45. obj.AddComponent(typeof(UnityWebRequest));
  46. _instance = obj.GetComponent<UnityWebRequest>();
  47. }
  48. return _instance;
  49. }
  50. }
  51. }
  52. #endregion
  53. /// <summary>
  54. /// 本次Request用的header
  55. /// </summary>
  56. Dictionary<string, string> headerDic = new Dictionary<string, string>();
  57. public class BackString
  58. {
  59. public string data;
  60. public string identifier;
  61. public GiveBackString del_GiveBack;
  62. public BackString(string data, string identifier, GiveBackString del_GiveBack)
  63. {
  64. this.data = data;
  65. this.identifier = identifier;
  66. this.del_GiveBack = del_GiveBack;
  67. }
  68. }
  69. public class BackStringArray
  70. {
  71. public string[] data;
  72. public string[] identifiers;
  73. public GiveBackStringArray del_GiveBack;
  74. public BackStringArray(string[] data, string[] identifiers, GiveBackStringArray del_GiveBack)
  75. {
  76. this.data = data;
  77. this.identifiers = identifiers;
  78. this.del_GiveBack = del_GiveBack;
  79. }
  80. }
  81. public class BackBytes
  82. {
  83. public byte[] data;
  84. public string identifier;
  85. public GiveBackBytes del_GiveBack;
  86. public BackBytes(byte[] data, string identifier, GiveBackBytes del_GiveBack)
  87. {
  88. this.data = data;
  89. this.identifier = identifier;
  90. this.del_GiveBack = del_GiveBack;
  91. }
  92. }
  93. public class BackBytesArray
  94. {
  95. public byte[][] data;
  96. public string[] identifiers;
  97. public GiveBackBytesArray del_GiveBack;
  98. public BackBytesArray(byte[][] data, string[] identifiers, GiveBackBytesArray del_GiveBack)
  99. {
  100. this.data = data;
  101. this.identifiers = identifiers;
  102. this.del_GiveBack = del_GiveBack;
  103. }
  104. }
  105. /// <summary>
  106. /// 用于所有文件上传完毕后的回调(除了请求的时候传入的回调,这个回调也是会被无条件回调的)
  107. /// </summary>
  108. public GiveBackString fileUploaded;
  109. #region 单请求
  110. /// <summary>
  111. /// 设置本次Request用的header,如果有多对header要设置,请多次调用
  112. /// </summary>
  113. /// <param name="key">Key.</param>
  114. /// <param name="value">Value.</param>
  115. public void SetRequestHeader(string key, string value)
  116. {
  117. try
  118. {
  119. headerDic.Add(key, value);
  120. }
  121. catch (System.Exception e)
  122. {
  123. Debug.Log(e.ToString());
  124. }
  125. }
  126. #region WWWForm表单
  127. /// <summary>
  128. /// 发送请求(POST)
  129. /// </summary>
  130. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  131. /// <param name="para">请求带的参数</param>
  132. public void Create(string urlKey, WWWForm para)
  133. {
  134. GiveBackString callBack = null;
  135. Create(urlKey, para, callBack, "", null);
  136. }
  137. /// <summary>
  138. /// 发送请求(POST)
  139. /// </summary>
  140. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  141. /// <param name="para">请求带的参数</param>
  142. /// <param name="identifier">请求标识符,identifier供用户分辨出这个返回数据是谁的</param>
  143. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  144. public void Create(string urlKey, WWWForm para, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  145. {
  146. GiveBackString callBack = null;
  147. Create(urlKey, para, callBack, identifier, giveBackLoadingProgress);
  148. }
  149. /// <summary>
  150. /// 发送请求(POST)
  151. /// </summary>
  152. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  153. /// <param name="para">请求带的参数</param>
  154. /// <param name="callBack">回调函数</param>
  155. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  156. public void Create(string urlKey, WWWForm para, GiveBackString callBack, string identifier)
  157. {
  158. StartCoroutine(WWWRequest(urlKey, para, callBack, identifier, null));
  159. }
  160. public void Create(string urlKey, WWWForm para, GiveBackBytes callBack, string identifier)
  161. {
  162. StartCoroutine(WWWRequest(urlKey, para, callBack, identifier, null));
  163. }
  164. /// <summary>
  165. /// 发送请求(POST)
  166. /// </summary>
  167. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  168. /// <param name="para">请求带的参数</param>
  169. /// <param name="callBack">回调函数</param>
  170. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  171. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  172. public void Create(string urlKey, WWWForm para, GiveBackString callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  173. {
  174. StartCoroutine(WWWRequest(urlKey, para, callBack, identifier, giveBackLoadingProgress));
  175. }
  176. public void Create(string urlKey, WWWForm para, GiveBackBytes callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  177. {
  178. StartCoroutine(WWWRequest(urlKey, para, callBack, identifier, giveBackLoadingProgress));
  179. }
  180. #endregion
  181. /// <summary>
  182. /// 发送请求
  183. /// </summary>
  184. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  185. /// <param name="requestMode">请求模式,GET/POST</param>
  186. public void Create(string urlKey, RequestMode requestMode)
  187. {
  188. GiveBackString callBack = null;
  189. Create(urlKey, "", requestMode, callBack, "", null);
  190. }
  191. /// <summary>
  192. /// 发送请求
  193. /// </summary>
  194. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  195. /// <param name="requestMode">请求模式,GET/POST</param>
  196. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  197. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  198. public void Create(string urlKey, RequestMode requestMode, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  199. {
  200. GiveBackString callBack = null;
  201. Create(urlKey, "", requestMode, callBack, identifier, giveBackLoadingProgress);
  202. }
  203. /// <summary>
  204. /// 发送请求
  205. /// </summary>
  206. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  207. /// <param name="requestMode">请求模式,GET/POST</param>
  208. /// <param name="callBack">回调函数</param>
  209. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  210. public void Create(string urlKey, RequestMode requestMode, GiveBackString callBack, string identifier)
  211. {
  212. Create(urlKey, "", requestMode, callBack, identifier, null);
  213. }
  214. public void Create(string urlKey, RequestMode requestMode, GiveBackBytes callBack, string identifier)
  215. {
  216. Create(urlKey, "", requestMode, callBack, identifier, null);
  217. }
  218. /// <summary>
  219. /// 发送请求
  220. /// </summary>
  221. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  222. /// <param name="requestMode">请求模式,GET/POST</param>
  223. /// <param name="callBack">回调函数</param>
  224. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  225. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  226. public void Create(string urlKey, RequestMode requestMode, GiveBackString callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  227. {
  228. Create(urlKey, "", requestMode, callBack, identifier, giveBackLoadingProgress);
  229. }
  230. public void Create(string urlKey, RequestMode requestMode, GiveBackBytes callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  231. {
  232. Create(urlKey, "", requestMode, callBack, identifier, giveBackLoadingProgress);
  233. }
  234. /// <summary>
  235. /// 发送请求
  236. /// </summary>
  237. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  238. /// <param name="para">请求带的参数,可以为空,如果是GET模式,此处的处理是参数直接追加到URL后面,建议GET模式下直接将参数写在URL中</param>
  239. /// <param name="requestMode">请求模式,GET/POST</param>
  240. public void Create(string urlKey, string para, RequestMode requestMode)
  241. {
  242. GiveBackString callBack = null;
  243. Create(urlKey, para, requestMode, callBack, "", null);
  244. }
  245. /// <summary>
  246. /// 发送请求
  247. /// </summary>
  248. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  249. /// <param name="para">请求带的参数,可以为空,如果是GET模式,此处的处理是参数直接追加到URL后面,建议GET模式下直接将参数写在URL中</param>
  250. /// <param name="requestMode">请求模式,GET/POST</param>
  251. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  252. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  253. public void Create(string urlKey, string para, RequestMode requestMode, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  254. {
  255. GiveBackString callBack = null;
  256. Create(urlKey, para, requestMode, callBack, identifier, giveBackLoadingProgress);
  257. }
  258. /// <summary>
  259. /// 发送请求
  260. /// </summary>
  261. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  262. /// <param name="para">请求带的参数,可以为空,如果是GET模式,此处的处理是参数直接追加到URL后面,建议GET模式下直接将参数写在URL中</param>
  263. /// <param name="requestMode">请求模式,GET/POST</param>
  264. /// <param name="callBack">回调函数</param>
  265. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  266. public void Create(string urlKey, string para, RequestMode requestMode, GiveBackString callBack, string identifier)
  267. {
  268. //Debug.Log("urlKey:" + urlKey + ", para:" + para);
  269. if (!string.IsNullOrEmpty(urlKey))
  270. {
  271. StartCoroutine(WWWRequest(urlKey, para, requestMode, callBack, identifier, null));
  272. }
  273. }
  274. public void Create(string urlKey, string para, RequestMode requestMode, GiveBackBytes callBack, string identifier)
  275. {
  276. //Debug.Log("urlKey:" + urlKey + ", para:" + para);
  277. if (!string.IsNullOrEmpty(urlKey))
  278. {
  279. StartCoroutine(WWWRequest(urlKey, para, requestMode, callBack, identifier, null));
  280. }
  281. }
  282. /// <summary>
  283. /// 发送请求
  284. /// </summary>
  285. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  286. /// <param name="para">请求带的参数,可以为空,如果是GET模式,此处的处理是参数直接追加到URL后面,建议GET模式下直接将参数写在URL中</param>
  287. /// <param name="requestMode">请求模式,GET/POST</param>
  288. /// <param name="callBack">回调函数</param>
  289. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  290. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  291. public void Create(string urlKey, string para, RequestMode requestMode, GiveBackString callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  292. {
  293. //Debug.Log("urlKey:" + urlKey + ", para:" + para);
  294. if (!string.IsNullOrEmpty(urlKey))
  295. {
  296. StartCoroutine(WWWRequest(urlKey, para, requestMode, callBack, identifier, giveBackLoadingProgress));
  297. }
  298. }
  299. public void Create(string urlKey, string para, RequestMode requestMode, GiveBackBytes callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  300. {
  301. //Debug.Log("urlKey:" + urlKey + ", para:" + para);
  302. if (!string.IsNullOrEmpty(urlKey))
  303. {
  304. StartCoroutine(WWWRequest(urlKey, para, requestMode, callBack, identifier, giveBackLoadingProgress));
  305. }
  306. }
  307. /// <summary>
  308. /// 请求一个string
  309. /// </summary>
  310. /// <returns>The request.</returns>
  311. /// <param name="urlKey">从WebRequest_URLs中的urlDic中取URL用,对应urlDic中的Key值</param>
  312. /// <param name="para">要传递的参数.</param>
  313. /// <param name="requestMode">请求模式,GET/POST</param>
  314. /// <param name="callBack">回调</param>
  315. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  316. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  317. public IEnumerator WWWRequest(string urlKey, string para, RequestMode requestMode, GiveBackString callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  318. {
  319. string url = GetURL(urlKey);
  320. url = (url == "" ? urlKey : url);
  321. string _para = (para == null ? "" : para);
  322. switch (requestMode)
  323. {
  324. case RequestMode.GET:
  325. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Get(url + _para))
  326. {
  327. if (giveBackLoadingProgress != null)
  328. {
  329. giveBackLoadingProgress(unityWebRequest, identifier);
  330. }
  331. yield return unityWebRequest.SendWebRequest();
  332. if (unityWebRequest.isNetworkError)
  333. {
  334. Debug.Log(unityWebRequest.error);
  335. }
  336. else
  337. {
  338. //Debug.Log("Response: " + unityWebRequest.downloadHandler.text + " Address:" + url);
  339. if (callBack != null)
  340. {
  341. callBack(unityWebRequest.downloadHandler.text, identifier, unityWebRequest.responseCode);
  342. }
  343. }
  344. }
  345. break;
  346. case RequestMode.POST:
  347. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = new UnityEngine.Networking.UnityWebRequest(url))
  348. {
  349. if (giveBackLoadingProgress != null)
  350. {
  351. giveBackLoadingProgress(unityWebRequest, identifier);
  352. }
  353. unityWebRequest.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(para));
  354. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  355. unityWebRequest.method = UnityEngine.Networking.UnityWebRequest.kHttpVerbPOST;
  356. if (headerDic.Count > 0)
  357. {
  358. foreach (KeyValuePair<string, string> itm in headerDic)
  359. {
  360. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  361. }
  362. headerDic.Clear();
  363. }
  364. //Debug.Log("Content-Type:" + unityWebRequest.GetRequestHeader("Content-Type"));
  365. yield return unityWebRequest.SendWebRequest();
  366. if (unityWebRequest.isNetworkError)
  367. {
  368. Debug.Log(unityWebRequest.error);
  369. }
  370. else
  371. {
  372. //Debug.Log("Response: " + unityWebRequest.downloadHandler.text + " Address:" + url);
  373. if (fileUploaded != null)
  374. {
  375. fileUploaded(unityWebRequest.downloadHandler.text, identifier);
  376. }
  377. if (callBack != null)
  378. {
  379. callBack(unityWebRequest.downloadHandler.text, identifier, unityWebRequest.responseCode);
  380. }
  381. }
  382. }
  383. break;
  384. }
  385. }
  386. /// <summary>
  387. /// 适用于键值对类型参数的POST请求
  388. /// </summary>
  389. /// <returns>The request.</returns>
  390. /// <param name="urlKey">从WebRequest_URLs中的urlDic中取URL用,对应urlDic中的Key值</param>
  391. /// <param name="para">要传递的参数</param>
  392. /// <param name="callBack">回调</param>
  393. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  394. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  395. public IEnumerator WWWRequest(string urlKey, WWWForm para, GiveBackString callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  396. {
  397. string url = GetURL(urlKey);
  398. url = (url == "" ? urlKey : url);
  399. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Post(url, para))
  400. {
  401. if (giveBackLoadingProgress != null)
  402. {
  403. giveBackLoadingProgress(unityWebRequest, identifier);
  404. }
  405. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  406. if (headerDic.Count > 0)
  407. {
  408. foreach (KeyValuePair<string, string> itm in headerDic)
  409. {
  410. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  411. }
  412. headerDic.Clear();
  413. }
  414. yield return unityWebRequest.SendWebRequest();
  415. if (unityWebRequest.isNetworkError)
  416. {
  417. Debug.Log(unityWebRequest.error);
  418. }
  419. else
  420. {
  421. //Debug.Log("Response: " + unityWebRequest.downloadHandler.text + " Address:" + url);
  422. if (fileUploaded != null)
  423. {
  424. fileUploaded(unityWebRequest.downloadHandler.text, identifier);
  425. }
  426. if (callBack != null)
  427. {
  428. callBack(unityWebRequest.downloadHandler.text, identifier, unityWebRequest.responseCode);
  429. }
  430. }
  431. }
  432. }
  433. /// <summary>
  434. /// 请求一个byte[]
  435. /// </summary>
  436. /// <returns>The request.</returns>
  437. /// <param name="urlKey">从WebRequest_URLs中的urlDic中取URL用,对应urlDic中的Key值</param>
  438. /// <param name="para">要传递的参数</param>
  439. /// <param name="requestMode">请求模式,GET/POST</param>
  440. /// <param name="callBack">回调</param>
  441. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  442. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  443. IEnumerator WWWRequest(string urlKey, string para, RequestMode requestMode, GiveBackBytes callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  444. {
  445. string url = GetURL(urlKey);
  446. url = (url == "" ? urlKey : url);
  447. string _para = (para == null ? "" : para);
  448. switch (requestMode)
  449. {
  450. case RequestMode.GET:
  451. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Get(url + _para))
  452. {
  453. if (giveBackLoadingProgress != null)
  454. {
  455. giveBackLoadingProgress(unityWebRequest, identifier);
  456. }
  457. yield return unityWebRequest.SendWebRequest();
  458. if (unityWebRequest.isNetworkError)
  459. {
  460. Debug.Log(unityWebRequest.error);
  461. }
  462. else
  463. {
  464. Debug.Log("Request complete!");
  465. if (callBack != null)
  466. {
  467. callBack(unityWebRequest.downloadHandler.data, identifier, unityWebRequest.responseCode);
  468. }
  469. }
  470. }
  471. break;
  472. case RequestMode.POST:
  473. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = new UnityEngine.Networking.UnityWebRequest(url))
  474. {
  475. if (giveBackLoadingProgress != null)
  476. {
  477. giveBackLoadingProgress(unityWebRequest, identifier);
  478. }
  479. unityWebRequest.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(para));
  480. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  481. unityWebRequest.method = UnityEngine.Networking.UnityWebRequest.kHttpVerbPOST;
  482. if (headerDic.Count > 0)
  483. {
  484. foreach (KeyValuePair<string, string> itm in headerDic)
  485. {
  486. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  487. }
  488. headerDic.Clear();
  489. }
  490. yield return unityWebRequest.SendWebRequest();
  491. if (unityWebRequest.isNetworkError)
  492. {
  493. Debug.Log(unityWebRequest.error);
  494. }
  495. else
  496. {
  497. Debug.Log("Request complete!");
  498. if (callBack != null)
  499. {
  500. callBack(unityWebRequest.downloadHandler.data, identifier, unityWebRequest.responseCode);
  501. }
  502. }
  503. }
  504. break;
  505. }
  506. }
  507. /// <summary>
  508. /// 适用于键值对类型参数的POST请求
  509. /// </summary>
  510. /// <returns>The request.</returns>
  511. /// <param name="urlKey">从WebRequest_URLs中的urlDic中取URL用,对应urlDic中的Key值</param>
  512. /// <param name="para">要传递的参数</param>
  513. /// <param name="callBack">回调</param>
  514. /// <param name="identifier">请求标识符,供用户分辨出这个返回数据是谁的</param>
  515. /// <param name="giveBackLoadingProgress">返回当前请求的加载进度</param>
  516. IEnumerator WWWRequest(string urlKey, WWWForm para, GiveBackBytes callBack, string identifier, GiveBackLoadingProgress giveBackLoadingProgress)
  517. {
  518. string url = GetURL(urlKey);
  519. url = (url == "" ? urlKey : url);
  520. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Post(url, para))
  521. {
  522. if (giveBackLoadingProgress != null)
  523. {
  524. giveBackLoadingProgress(unityWebRequest, identifier);
  525. }
  526. unityWebRequest.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(para.data);
  527. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  528. unityWebRequest.method = UnityEngine.Networking.UnityWebRequest.kHttpVerbPOST;
  529. if (headerDic.Count > 0)
  530. {
  531. foreach (KeyValuePair<string, string> itm in headerDic)
  532. {
  533. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  534. }
  535. headerDic.Clear();
  536. }
  537. yield return unityWebRequest.SendWebRequest();
  538. if (unityWebRequest.isNetworkError)
  539. {
  540. Debug.Log(unityWebRequest.error);
  541. }
  542. else
  543. {
  544. Debug.Log("Request complete!");
  545. if (callBack != null)
  546. {
  547. callBack(unityWebRequest.downloadHandler.data, identifier, unityWebRequest.responseCode);
  548. }
  549. }
  550. }
  551. }
  552. #endregion
  553. #region 多请求
  554. public class OneGroup
  555. {
  556. public string urlKey;
  557. public string para;
  558. public RequestMode requestMode;
  559. public string identifier;
  560. public OneGroup(string _urlKey, string _para, RequestMode _requestMode, string _identifier)
  561. {
  562. urlKey = _urlKey;
  563. para = _para;
  564. requestMode = _requestMode;
  565. identifier = _identifier;
  566. }
  567. }
  568. List<OneGroup> requestsDic = new List<OneGroup>();
  569. /// <summary>
  570. /// 添加请求(POST)
  571. /// </summary>
  572. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  573. /// <param name="para">请求带的参数</param>
  574. public void Add(string urlKey, WWWForm para, string identifier)
  575. {
  576. }
  577. /// <summary>
  578. /// 添加请求
  579. /// </summary>
  580. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  581. /// <param name="requestMode">请求模式,GET/POST</param>
  582. public void Add(string urlKey, RequestMode requestMode, string identifier)
  583. {
  584. Add(urlKey, "", requestMode, identifier);
  585. }
  586. /// <summary>
  587. /// 添加请求
  588. /// </summary>
  589. /// <param name="urlKey">此处可以直接填写url,也可以填写WebRequest_URLs中urlDic中的Key值</param>
  590. /// <param name="para">请求带的参数,可以为空,如果是GET模式,此处的处理是参数直接追加到URL后面,建议GET模式下直接将参数写在URL中</param>
  591. /// <param name="requestMode">请求模式,GET/POST</param>
  592. public void Add(string urlKey, string para, RequestMode requestMode, string identifier)
  593. {
  594. requestsDic.Add(new OneGroup(urlKey, para, requestMode, identifier));
  595. }
  596. /// <summary>
  597. /// 清空所有已经Add进来的请求
  598. /// </summary>
  599. public void Clear()
  600. {
  601. requestsDic.Clear();
  602. }
  603. /// <summary>
  604. /// 一次性执行所有已经Add进来的请求,并在所有请求处理完毕后回调callBack
  605. /// </summary>
  606. /// <param name="callBack">Call back.</param>
  607. public void CreateBatch(GiveBackStringArray callBack)
  608. {
  609. if (requestsDic != null)
  610. {
  611. StartCoroutine(BatchRequest(callBack, requestsDic.ToArray()));
  612. }
  613. }
  614. /// <summary>
  615. /// 一次性执行所有已经Add进来的请求,并在所有请求处理完毕后回调callBack
  616. /// </summary>
  617. /// <param name="callBack">Call back.</param>
  618. public void CreateBatch(GiveBackBytesArray callBack)
  619. {
  620. if (requestsDic != null)
  621. {
  622. StartCoroutine(BatchRequest(callBack, requestsDic.ToArray()));
  623. }
  624. }
  625. /// <summary>
  626. /// 创建多个request(GiveBackStringArray)
  627. /// </summary>
  628. /// <returns>The request.</returns>
  629. /// <param name="callBack">回调</param>
  630. IEnumerator BatchRequest(GiveBackStringArray callBack, OneGroup[] requestsArray)
  631. {
  632. string[] identifiers = new string[requestsArray.Length];
  633. string[] tempData = new string[requestsArray.Length];
  634. for (int i = 0; i < requestsArray.Length; i++)
  635. {
  636. identifiers[i] = requestsArray[i].identifier;
  637. string url = GetURL(requestsArray[i].urlKey);
  638. url = (url == "" ? requestsArray[i].urlKey : url);
  639. string _para = (requestsArray[i].para == null ? "" : requestsArray[i].para);
  640. switch (requestsArray[i].requestMode)
  641. {
  642. case RequestMode.GET:
  643. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Get(url + _para))
  644. {
  645. yield return unityWebRequest.SendWebRequest();
  646. if (unityWebRequest.isNetworkError)
  647. {
  648. Debug.Log(unityWebRequest.error);
  649. }
  650. else
  651. {
  652. Debug.Log("Response: " + unityWebRequest.downloadHandler.text + " Address:" + url);
  653. tempData[i] = unityWebRequest.downloadHandler.text;
  654. }
  655. }
  656. break;
  657. case RequestMode.POST:
  658. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = new UnityEngine.Networking.UnityWebRequest(url))
  659. {
  660. unityWebRequest.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(requestsArray[i].para));
  661. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  662. unityWebRequest.method = UnityEngine.Networking.UnityWebRequest.kHttpVerbPOST;
  663. if (headerDic.Count > 0)
  664. {
  665. foreach (KeyValuePair<string, string> itm in headerDic)
  666. {
  667. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  668. }
  669. headerDic.Clear();
  670. }
  671. //Debug.Log("Content-Type:" + unityWebRequest.GetRequestHeader("Content-Type"));
  672. yield return unityWebRequest.SendWebRequest();
  673. if (unityWebRequest.isNetworkError)
  674. {
  675. Debug.Log(unityWebRequest.error);
  676. }
  677. else
  678. {
  679. //Debug.Log("Response: " + unityWebRequest.downloadHandler.text + " Address:" + url);
  680. tempData[i] = unityWebRequest.downloadHandler.text;
  681. }
  682. }
  683. break;
  684. }
  685. }
  686. Clear();
  687. if (callBack != null)
  688. {
  689. callBack(tempData, identifiers);
  690. }
  691. }
  692. /// <summary>
  693. /// 创建多个request(GiveBackBytesArray)
  694. /// </summary>
  695. /// <returns>The request.</returns>
  696. /// <param name="callBack">回调</param>
  697. IEnumerator BatchRequest(GiveBackBytesArray callBack, OneGroup[] requestsArray)
  698. {
  699. string[] identifiers = new string[requestsArray.Length];
  700. byte[][] tempData = new byte[requestsArray.Length][];
  701. long[] statusCodes = new long[requestsArray.Length];
  702. for (int i = 0; i < requestsArray.Length; i++)
  703. {
  704. identifiers[i] = requestsArray[i].identifier;
  705. string url = GetURL(requestsArray[i].urlKey);
  706. url = (url == "" ? requestsArray[i].urlKey : url);
  707. string _para = (requestsArray[i].para == null ? "" : requestsArray[i].para);
  708. switch (requestsArray[i].requestMode)
  709. {
  710. case RequestMode.GET:
  711. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = UnityEngine.Networking.UnityWebRequest.Get(url + _para))
  712. {
  713. yield return unityWebRequest.SendWebRequest();
  714. if (unityWebRequest.isNetworkError)
  715. {
  716. Debug.Log(unityWebRequest.error);
  717. }
  718. else
  719. {
  720. tempData[i] = new byte[unityWebRequest.downloadHandler.data.Length];
  721. tempData[i] = unityWebRequest.downloadHandler.data;
  722. statusCodes[i] = unityWebRequest.responseCode;
  723. }
  724. }
  725. break;
  726. case RequestMode.POST:
  727. using (UnityEngine.Networking.UnityWebRequest unityWebRequest = new UnityEngine.Networking.UnityWebRequest(url))
  728. {
  729. unityWebRequest.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(requestsArray[i].para));
  730. unityWebRequest.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
  731. unityWebRequest.method = UnityEngine.Networking.UnityWebRequest.kHttpVerbPOST;
  732. if (headerDic.Count > 0)
  733. {
  734. foreach (KeyValuePair<string, string> itm in headerDic)
  735. {
  736. unityWebRequest.SetRequestHeader(itm.Key, itm.Value);
  737. }
  738. headerDic.Clear();
  739. }
  740. //Debug.Log("Content-Type:" + unityWebRequest.GetRequestHeader("Content-Type"));
  741. yield return unityWebRequest.SendWebRequest();
  742. if (unityWebRequest.isNetworkError)
  743. {
  744. Debug.Log(unityWebRequest.error);
  745. }
  746. else
  747. {
  748. //Debug.Log("Respon Bytes Length: " + unityWebRequest.downloadHandler.data.Length + " Address:" + url);
  749. tempData[i] = new byte[unityWebRequest.downloadHandler.data.Length];
  750. tempData[i] = unityWebRequest.downloadHandler.data;
  751. statusCodes[i] = unityWebRequest.responseCode;
  752. }
  753. }
  754. break;
  755. }
  756. }
  757. Clear();
  758. if (callBack != null)
  759. {
  760. callBack(tempData, identifiers, statusCodes);
  761. }
  762. }
  763. #endregion
  764. }
  765. }