CosResult.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Network;
  5. using System.IO;
  6. using COSXML.Transfer;
  7. using COSXML.Model.Object;
  8. namespace COSXML.Model
  9. {
  10. public class CosResult
  11. {
  12. public string Key { get; protected set; }
  13. /// <summary>
  14. /// http code
  15. /// </summary>
  16. public int httpCode;
  17. /// <summary>
  18. /// http message
  19. /// </summary>
  20. public string httpMessage;
  21. /// <summary>
  22. /// http response headers
  23. /// </summary>
  24. public Dictionary<string, List<string>> responseHeaders;
  25. /// <summary>
  26. /// raw http response body
  27. /// </summary>
  28. public string RawContentBodyString { set; get; }
  29. /// <summary>
  30. /// check successful
  31. /// </summary>
  32. /// <returns></returns>
  33. public bool IsSuccessful()
  34. {
  35. return httpCode >= 200 && httpCode < 300;
  36. }
  37. /// <summary>
  38. /// exchange infor between request and result
  39. /// </summary>
  40. /// <param name="cosRequest"></param>
  41. internal virtual void ExternInfo(CosRequest cosRequest)
  42. {
  43. if (cosRequest is ObjectRequest)
  44. {
  45. Key = ((ObjectRequest)cosRequest).Key;
  46. }
  47. }
  48. /// <summary>
  49. /// parse status line and headers
  50. /// </summary>
  51. internal virtual void InternalParseResponseHeaders()
  52. {
  53. }
  54. /// <summary>
  55. /// parse response body, such as download files.
  56. /// </summary>
  57. /// <param name="inputStream"> input stream </param>
  58. /// <param name="contentType"> body mime type</param>
  59. /// <param name="contentLength">body length</param>
  60. internal virtual void ParseResponseBody(Stream inputStream, string contentType, long contentLength)
  61. {
  62. }
  63. /// <summary>
  64. /// get result message
  65. /// </summary>
  66. /// <returns></returns>
  67. public virtual string GetResultInfo()
  68. {
  69. StringBuilder resultBuilder = new StringBuilder();
  70. resultBuilder.Append(httpCode).Append(" ").Append(httpMessage).Append("\n");
  71. if (responseHeaders != null)
  72. {
  73. foreach (KeyValuePair<string, List<string>> element in responseHeaders)
  74. {
  75. resultBuilder.Append(element.Key).Append(": ").Append(element.Value[0]).Append("\n");
  76. }
  77. }
  78. return resultBuilder.ToString();
  79. }
  80. }
  81. public class CosDataResult<T> : CosResult
  82. {
  83. /// <summary>
  84. /// body数据
  85. /// </summary>
  86. protected T _data;
  87. internal override void ParseResponseBody(Stream inputStream, string contentType, long contentLength)
  88. {
  89. if (contentLength != 0)
  90. {
  91. _data = XmlParse.Deserialize<T>(inputStream);
  92. }
  93. }
  94. public override string GetResultInfo()
  95. {
  96. var info = base.GetResultInfo();
  97. var methodInfo = typeof(T).GetMethod("GetInfo");
  98. if (methodInfo != null && _data != null)
  99. {
  100. info = info + "\n" + methodInfo.Invoke(_data, null);
  101. }
  102. return info;
  103. }
  104. }
  105. }