2 // System.Net.HttpWebResponse
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
10 using System.Runtime.Serialization;
\r
12 namespace System.Net
\r
15 public class HttpWebResponse : WebResponse, ISerializable, IDisposable
\r
18 private WebHeaderCollection webHeaders;
\r
19 private CookieCollection cookieCollection = null;
\r
20 private string method = null;
\r
21 private Version version = null;
\r
22 private HttpStatusCode statusCode;
\r
23 private string statusDescription = null;
\r
25 private Stream responseStream;
\r
26 private bool disposed = false;
\r
30 internal HttpWebResponse (Uri uri, string method, Stream responseStream)
\r
33 this.method = method;
\r
34 this.responseStream = responseStream;
\r
36 // TODO: parse headers from responseStream
\r
38 this.statusCode = HttpStatusCode.OK;
\r
41 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
43 throw new NotSupportedException ();
\r
48 public string CharacterSet {
\r
49 // Content-Type = "Content-Type" ":" media-type
\r
50 // media-type = type "/" subtype *( ";" parameter )
\r
51 // parameter = attribute "=" value
\r
52 // 3.7.1. default is ISO-8859-1
\r
55 string contentType = ContentType;
\r
56 if (contentType == null)
\r
57 return "ISO-8859-1";
\r
58 string val = contentType.ToLower ();
\r
59 int pos = val.IndexOf ("charset=");
\r
61 return "ISO-8859-1";
\r
63 int pos2 = val.IndexOf (';', pos);
\r
65 ? contentType.Substring (pos)
\r
66 : contentType.Substring (pos, pos2 - pos);
\r
73 public string ContentEncoding {
\r
75 try { return webHeaders ["Content-Encoding"]; }
\r
76 finally { CheckDisposed (); }
\r
80 public override long ContentLength {
\r
83 return Int64.Parse (webHeaders ["Content-Length"]);
\r
84 } catch (Exception) {
\r
92 public override string ContentType {
\r
94 try { return webHeaders ["Content-Type"]; }
\r
95 finally { CheckDisposed (); }
\r
99 public CookieCollection Cookies {
\r
103 // LAMESPEC: a simple test reveal this always
\r
104 // returns an empty collection. It is not filled
\r
105 // with the values from the Set-Cookie or
\r
106 // Set-Cookie2 response headers, which is a bit
\r
108 if (cookieCollection == null)
\r
109 cookieCollection = new CookieCollection ();
\r
110 return cookieCollection;
\r
114 // ?? don't understand how you can set cookies on a response.
\r
115 throw new NotSupportedException ();
\r
119 public override WebHeaderCollection Headers {
\r
122 return webHeaders;
\r
126 public DateTime LastModified {
\r
130 string dtStr = webHeaders ["Last-Modified"];
\r
131 return MonoHttpDate.Parse (dtStr);
\r
132 } catch (Exception) {
\r
133 return DateTime.Now;
\r
138 public string Method {
\r
145 public Version ProtocolVersion {
\r
152 public override Uri ResponseUri {
\r
159 public string Server {
\r
162 return webHeaders ["Server"];
\r
169 public HttpStatusCode StatusCode {
\r
172 return statusCode;
\r
176 public string StatusDescription {
\r
179 return statusDescription;
\r
185 public override int GetHashCode ()
\r
188 return base.GetHashCode ();
\r
194 public string GetResponseHeader (string headerName)
\r
197 return webHeaders [headerName];
\r
203 public override Stream GetResponseStream ()
\r
206 if (method.Equals ("HEAD")) // see par 4.3 & 9.4
\r
207 return Stream.Null;
\r
208 return responseStream;
\r
215 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
\r
216 StreamingContext streamingContext)
\r
219 throw new NotImplementedException ();
\r
223 // Cleaning up stuff
\r
225 ~HttpWebResponse ()
\r
230 public override void Close ()
\r
232 ((IDisposable) this).Dispose ();
\r
235 void IDisposable.Dispose ()
\r
238 GC.SuppressFinalize (this);
\r
241 protected virtual void Dispose (bool disposing)
\r
245 this.disposed = true;
\r
248 // release managed resources
\r
251 cookieCollection = null;
\r
254 // statusCode = null;
\r
255 statusDescription = null;
\r
258 // release unmanaged resources
\r
259 Stream stream = responseStream;
\r
260 responseStream = null;
\r
261 if (stream != null)
\r
262 stream.Close (); // also closes webRequest
\r
265 private void CheckDisposed ()
\r
268 throw new ObjectDisposedException (GetType ().FullName);
\r