HttpResponseMessage.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // HttpResponseMessage.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Net.Http.Headers;
  29. using System.Text;
  30. namespace System.Net.Http
  31. {
  32. public class HttpResponseMessage : IDisposable
  33. {
  34. HttpResponseHeaders headers;
  35. HttpResponseHeaders trailingHeaders;
  36. string reasonPhrase;
  37. HttpStatusCode statusCode;
  38. Version version;
  39. bool disposed;
  40. public HttpResponseMessage ()
  41. : this (HttpStatusCode.OK)
  42. {
  43. }
  44. public HttpResponseMessage (HttpStatusCode statusCode)
  45. {
  46. StatusCode = statusCode;
  47. }
  48. public HttpContent Content { get; set; }
  49. public HttpResponseHeaders Headers {
  50. get {
  51. return headers ?? (headers = new HttpResponseHeaders ());
  52. }
  53. }
  54. public bool IsSuccessStatusCode {
  55. get {
  56. // Successful codes are 2xx
  57. return statusCode >= HttpStatusCode.OK && statusCode < HttpStatusCode.MultipleChoices;
  58. }
  59. }
  60. public string ReasonPhrase {
  61. get {
  62. return reasonPhrase ?? HttpStatusDescription.Get (statusCode);
  63. }
  64. set {
  65. reasonPhrase = value;
  66. }
  67. }
  68. public HttpRequestMessage RequestMessage { get; set; }
  69. public HttpStatusCode StatusCode {
  70. get {
  71. return statusCode;
  72. }
  73. set {
  74. if (value < 0)
  75. throw new ArgumentOutOfRangeException ();
  76. statusCode = value;
  77. }
  78. }
  79. public Version Version {
  80. get {
  81. return version ?? HttpVersion.Version11;
  82. }
  83. set {
  84. if (value == null)
  85. throw new ArgumentNullException ("Version");
  86. version = value;
  87. }
  88. }
  89. public void Dispose ()
  90. {
  91. Dispose (true);
  92. }
  93. protected virtual void Dispose (bool disposing)
  94. {
  95. if (disposing && !disposed) {
  96. disposed = true;
  97. if (Content != null)
  98. Content.Dispose ();
  99. }
  100. }
  101. public HttpResponseMessage EnsureSuccessStatusCode ()
  102. {
  103. if (IsSuccessStatusCode)
  104. return this;
  105. throw new HttpRequestException (string.Format ("{0} ({1})", (int) statusCode, ReasonPhrase));
  106. }
  107. public override string ToString ()
  108. {
  109. var sb = new StringBuilder ();
  110. sb.Append ("StatusCode: ").Append ((int)StatusCode);
  111. sb.Append (", ReasonPhrase: '").Append (ReasonPhrase ?? "<null>");
  112. sb.Append ("', Version: ").Append (Version);
  113. sb.Append (", Content: ").Append (Content != null ? Content.ToString () : "<null>");
  114. sb.Append (", Headers:\r\n{\r\n").Append (Headers);
  115. if (Content != null)
  116. sb.Append (Content.Headers);
  117. sb.Append ("}");
  118. return sb.ToString ();
  119. }
  120. public HttpResponseHeaders TrailingHeaders {
  121. get {
  122. if (trailingHeaders == null)
  123. trailingHeaders = new HttpResponseHeaders ();
  124. return trailingHeaders;
  125. }
  126. }
  127. }
  128. }