HttpGlobal.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using BestHTTP;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. public class HttpGlobal : MonoBehaviour
  10. {
  11. public class LogBody {
  12. public string msg;
  13. public LogBody(string newMsg) {
  14. msg = newMsg;
  15. }
  16. }
  17. public static void SendLogToServer(string logContent) {
  18. logContent = string.Format("用户【{0}】", UserData.realName) + logContent;
  19. Debug.Log(HttpAddress.logAddress);
  20. BestHTTP.HTTPRequest request = new BestHTTP.HTTPRequest(new System.Uri(HttpAddress.logAddress), BestHTTP.HTTPMethods.Post, (HTTPRequest request1, HTTPResponse response) =>
  21. {
  22. try
  23. {
  24. Debug.Log(response.DataAsText);
  25. LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
  26. if (loginInfo.code == 200)
  27. {
  28. Debug.Log("发送日志成功,Content:" + logContent.ToString());
  29. }
  30. else {
  31. Debug.Log("发送日志失败,Reason:" + loginInfo.msg);
  32. }
  33. }
  34. catch (Exception e)
  35. {
  36. Debug.Log("发送日志失败,Reason:" + e.ToString());
  37. }
  38. });
  39. string jsonContent = JsonConvert.SerializeObject(new LogBody(logContent));
  40. request.RawData = Encoding.UTF8.GetBytes(jsonContent);
  41. request.AddHeader("token", UserData.token);
  42. request.AddHeader("Content-Type", "application/json");
  43. request.Send();
  44. }
  45. public static void SendPingToServer()
  46. {
  47. Debug.Log(HttpAddress.logAddress);
  48. BestHTTP.HTTPRequest request = new BestHTTP.HTTPRequest(new System.Uri(HttpAddress.pingAddress), BestHTTP.HTTPMethods.Post, (HTTPRequest request1, HTTPResponse response) =>
  49. {
  50. try
  51. {
  52. LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
  53. if (loginInfo.code == 200)
  54. {
  55. Debug.Log("发送Ping成功");
  56. }
  57. else
  58. {
  59. Debug.Log("发送Ping失败,Reason:" + loginInfo.msg);
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. Debug.Log("发送Ping失败,Reason:" + e.ToString());
  65. }
  66. });
  67. request.AddHeader("token", UserData.token);
  68. request.AddHeader("Content-Type", "application/json");
  69. request.Send();
  70. }
  71. }