HttpWebResponse.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // System.Net.HttpWebResponse
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. namespace System.Net
  11. {
  12. [Serializable]
  13. public class HttpWebResponse : WebResponse, ISerializable, IDisposable
  14. {
  15. private Uri uri;
  16. private WebHeaderCollection webHeaders;
  17. private CookieCollection cookieCollection = null;
  18. private string method = null;
  19. private Version version = null;
  20. private HttpStatusCode statusCode;
  21. private string statusDescription = null;
  22. private Stream responseStream;
  23. private bool disposed = false;
  24. // Constructors
  25. protected HttpWebResponse (Uri uri, Stream responseStream)
  26. {
  27. this.uri = uri;
  28. this.responseStream = responseStream;
  29. }
  30. protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  31. {
  32. throw new NotSupportedException ();
  33. }
  34. // Properties
  35. public string CharacterSet {
  36. // Content-Type = "Content-Type" ":" media-type
  37. // media-type = type "/" subtype *( ";" parameter )
  38. // parameter = attribute "=" value
  39. // 3.7.1. default is ISO-8859-1
  40. get {
  41. CheckDisposed ();
  42. string contentType = ContentType;
  43. if (contentType == null)
  44. return "ISO-8859-1";
  45. string val = contentType.ToLower ();
  46. int pos = val.IndexOf ("charset=");
  47. if (pos == -1)
  48. return "ISO-8859-1";
  49. pos += 8;
  50. int pos2 = val.IndexOf (';', pos);
  51. return (pos2 == -1)
  52. ? contentType.Substring (pos)
  53. : contentType.Substring (pos, pos2 - pos);
  54. }
  55. }
  56. public string ContentEncoding {
  57. get {
  58. CheckDisposed ();
  59. return webHeaders ["Content-Encoding"];
  60. }
  61. }
  62. public override long ContentLength {
  63. get {
  64. CheckDisposed ();
  65. try {
  66. return Int64.Parse (webHeaders ["Content-Length"]);
  67. } catch (Exception) {
  68. return -1;
  69. }
  70. }
  71. }
  72. public override string ContentType {
  73. get {
  74. CheckDisposed ();
  75. return webHeaders ["Content-Type"];
  76. }
  77. }
  78. public CookieCollection Cookies {
  79. get {
  80. CheckDisposed ();
  81. // LAMESPEC: a simple test reveal this always
  82. // returns an empty collection. It is not filled
  83. // with the values from the Set-Cookie or
  84. // Set-Cookie2 response headers, which is a bit
  85. // of a shame..
  86. if (cookieCollection == null)
  87. cookieCollection = new CookieCollection ();
  88. return cookieCollection;
  89. }
  90. set {
  91. CheckDisposed ();
  92. // ?? don't understand how you can set cookies on a response.
  93. throw new NotSupportedException ();
  94. }
  95. }
  96. public override WebHeaderCollection Headers {
  97. get {
  98. CheckDisposed ();
  99. return webHeaders;
  100. }
  101. }
  102. public DateTime LastModified {
  103. get {
  104. CheckDisposed ();
  105. try {
  106. string dtStr = webHeaders ["Last-Modified"];
  107. // TODO: accept more than rfc1123 dates
  108. DateTime dt = DateTime.ParseExact (dtStr, "r", null);
  109. return dt;
  110. } catch (Exception) {
  111. return DateTime.Now;
  112. }
  113. }
  114. }
  115. public string Method {
  116. get {
  117. CheckDisposed ();
  118. return method;
  119. }
  120. }
  121. public Version ProtocolVersion {
  122. get {
  123. CheckDisposed ();
  124. return version;
  125. }
  126. }
  127. public override Uri ResponseUri {
  128. get {
  129. CheckDisposed ();
  130. return uri;
  131. }
  132. }
  133. public string Server {
  134. get {
  135. CheckDisposed ();
  136. return webHeaders ["Server"];
  137. }
  138. }
  139. public HttpStatusCode StatusCode {
  140. get {
  141. CheckDisposed ();
  142. return statusCode;
  143. }
  144. }
  145. public string StatusDescription {
  146. get {
  147. CheckDisposed ();
  148. return statusDescription;
  149. }
  150. }
  151. // Methods
  152. public override int GetHashCode ()
  153. {
  154. CheckDisposed ();
  155. return base.GetHashCode ();
  156. }
  157. public string GetResponseHeader (string headerName)
  158. {
  159. CheckDisposed ();
  160. return webHeaders [headerName];
  161. }
  162. [MonoTODO]
  163. public override Stream GetResponseStream ()
  164. {
  165. CheckDisposed ();
  166. throw new NotImplementedException ();
  167. }
  168. [MonoTODO]
  169. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  170. StreamingContext streamingContext)
  171. {
  172. CheckDisposed ();
  173. throw new NotImplementedException ();
  174. }
  175. // Cleaning up stuff
  176. ~HttpWebResponse ()
  177. {
  178. Dispose (false);
  179. }
  180. public override void Close ()
  181. {
  182. ((IDisposable) this).Dispose ();
  183. }
  184. void IDisposable.Dispose ()
  185. {
  186. Dispose (true);
  187. // see spec, suppress finalization of this object.
  188. GC.SuppressFinalize (this);
  189. }
  190. protected virtual void Dispose (bool disposing)
  191. {
  192. if (this.disposed)
  193. return;
  194. this.disposed = true;
  195. if (disposing) {
  196. // release managed resources
  197. uri = null;
  198. webHeaders = null;
  199. cookieCollection = null;
  200. method = null;
  201. version = null;
  202. // statusCode = null;
  203. statusDescription = null;
  204. }
  205. // release unmanaged resources
  206. Stream stream = responseStream;
  207. responseStream = null;
  208. if (stream != null)
  209. stream.Close ();
  210. }
  211. private void CheckDisposed ()
  212. {
  213. if (disposed)
  214. throw new ObjectDisposedException ("HttpWebResponse");
  215. }
  216. }
  217. }