HttpResponse.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using UnityEngine;
  2. namespace EZXR.Glass.Network.Http
  3. {
  4. /**
  5. * \~english
  6. * Http response class
  7. *
  8. * \~chinese
  9. * Http应答类
  10. */
  11. public class HttpResponse {
  12. /**
  13. * \~english
  14. * Status code
  15. *
  16. * \~chinese
  17. * 状态码
  18. */
  19. public long status;
  20. /**
  21. * \~english
  22. * Http response body
  23. *
  24. * \~chinese
  25. * Http回答返回体
  26. */
  27. public string body;
  28. // public byte[] rawBody;
  29. /**
  30. * \~english
  31. * Http response error message
  32. *
  33. * \~chinese
  34. * Http回答的错误信息
  35. */
  36. public string error;
  37. public HttpResponse(long status, string body) {
  38. this.status = status;
  39. this.body = body;
  40. // this.rawBody = rawBody;
  41. this.error = null;
  42. }
  43. public HttpResponse(string error) {
  44. this.error = error;
  45. }
  46. // public T To<T>() {
  47. // return JsonUtility.FromJson<T> (body);
  48. // }
  49. /**
  50. * \~english
  51. * @brief Get the status of Http response, refer to Http status code
  52. *
  53. * \~chinese
  54. * @brief 获取Http回答的状态,参考Http状态码
  55. *
  56. */
  57. public long Status() {
  58. return status;
  59. }
  60. /**
  61. * \~english
  62. * @brief Get Http response body
  63. *
  64. * \~chinese
  65. * @brief 获取Http回答返回体
  66. *
  67. */
  68. public string Body() {
  69. return body;
  70. }
  71. // public byte[] RawBody() {
  72. // return rawBody;
  73. // }
  74. /**
  75. * \~english
  76. * @brief Determine whether the Http response is wrong
  77. *
  78. * \~chinese
  79. * @brief 判断Http回答是否有错误
  80. *
  81. */
  82. public bool IsOK() {
  83. return status >= 200 && status < 300 && error == null;
  84. }
  85. /**
  86. * \~english
  87. * @brief Determine whether the Http response is wrong
  88. *
  89. * \~chinese
  90. * @brief 获取Http回答的错误信息
  91. *
  92. */
  93. public string Error() {
  94. return error;
  95. }
  96. public override string ToString()
  97. {
  98. return "status: " + status.ToString () + " - response: " + body.ToString ();
  99. }
  100. }
  101. }