mscorlib(4.0.0.0) API with additions
HttpWebResponse.cs
3 using System.IO;
4 using System.IO.Compression;
5 using System.Net.WebSockets;
9 
10 namespace System.Net
11 {
13  [Serializable]
14  [global::__DynamicallyInvokable]
16  {
17  private Uri m_Uri;
18 
19  private KnownHttpVerb m_Verb;
20 
21  private HttpStatusCode m_StatusCode;
22 
23  private string m_StatusDescription;
24 
25  private Stream m_ConnectStream;
26 
27  private CoreResponseData m_CoreResponseData;
28 
29  private WebHeaderCollection m_HttpResponseHeaders;
30 
31  private long m_ContentLength;
32 
33  private string m_MediaType;
34 
35  private string m_CharacterSet;
36 
37  private bool m_IsVersionHttp11;
38 
39  internal X509Certificate m_Certificate;
40 
41  private CookieCollection m_cookies;
42 
43  private bool m_disposed;
44 
45  private bool m_propertiesDisposed;
46 
47  private bool m_UsesProxySemantics;
48 
49  private bool m_IsMutuallyAuthenticated;
50 
51  private bool m_IsWebSocketResponse;
52 
53  private string m_ConnectionGroupName;
54 
55  private Stream m_WebSocketConnectionStream;
56 
57  internal bool IsWebSocketResponse
58  {
59  get
60  {
61  return m_IsWebSocketResponse;
62  }
63  set
64  {
65  m_IsWebSocketResponse = value;
66  }
67  }
68 
69  internal string ConnectionGroupName
70  {
71  get
72  {
73  return m_ConnectionGroupName;
74  }
75  set
76  {
77  m_ConnectionGroupName = value;
78  }
79  }
80 
81  internal Stream ResponseStream
82  {
83  get
84  {
85  return m_ConnectStream;
86  }
87  set
88  {
89  m_ConnectStream = value;
90  }
91  }
92 
93  internal CoreResponseData CoreResponseData => m_CoreResponseData;
94 
98  public override bool IsMutuallyAuthenticated
99  {
100  get
101  {
102  CheckDisposed();
103  return m_IsMutuallyAuthenticated;
104  }
105  }
106 
107  internal bool InternalSetIsMutuallyAuthenticated
108  {
109  set
110  {
111  m_IsMutuallyAuthenticated = value;
112  }
113  }
114 
118  [global::__DynamicallyInvokable]
119  public virtual CookieCollection Cookies
120  {
121  [global::__DynamicallyInvokable]
122  get
123  {
124  CheckDisposed();
125  if (m_cookies == null)
126  {
127  m_cookies = new CookieCollection();
128  }
129  return m_cookies;
130  }
131  set
132  {
133  CheckDisposed();
134  m_cookies = value;
135  }
136  }
137 
141  [global::__DynamicallyInvokable]
142  public override WebHeaderCollection Headers
143  {
144  [global::__DynamicallyInvokable]
145  get
146  {
147  CheckDisposed();
148  return m_HttpResponseHeaders;
149  }
150  }
151 
155  [global::__DynamicallyInvokable]
156  public override bool SupportsHeaders
157  {
158  [global::__DynamicallyInvokable]
159  get
160  {
161  return true;
162  }
163  }
164 
168  [global::__DynamicallyInvokable]
169  public override long ContentLength
170  {
171  [global::__DynamicallyInvokable]
172  get
173  {
174  CheckDisposed();
175  return m_ContentLength;
176  }
177  }
178 
182  public string ContentEncoding
183  {
184  get
185  {
186  CheckDisposed();
187  string text = m_HttpResponseHeaders["Content-Encoding"];
188  if (text != null)
189  {
190  return text;
191  }
192  return string.Empty;
193  }
194  }
195 
199  [global::__DynamicallyInvokable]
200  public override string ContentType
201  {
202  [global::__DynamicallyInvokable]
203  get
204  {
205  CheckDisposed();
206  string contentType = m_HttpResponseHeaders.ContentType;
207  if (contentType != null)
208  {
209  return contentType;
210  }
211  return string.Empty;
212  }
213  }
214 
218  public string CharacterSet
219  {
220  get
221  {
222  CheckDisposed();
223  string contentType = m_HttpResponseHeaders.ContentType;
224  if (m_CharacterSet == null && !ValidationHelper.IsBlankString(contentType))
225  {
226  m_CharacterSet = string.Empty;
227  string text = contentType.ToLower(CultureInfo.InvariantCulture);
228  if (text.Trim().StartsWith("text/"))
229  {
230  m_CharacterSet = "ISO-8859-1";
231  }
232  int i = text.IndexOf(";");
233  if (i > 0)
234  {
235  while ((i = text.IndexOf("charset", i)) >= 0)
236  {
237  i += 7;
238  if (text[i - 8] != ';' && text[i - 8] != ' ')
239  {
240  continue;
241  }
242  for (; i < text.Length && text[i] == ' '; i++)
243  {
244  }
245  if (i < text.Length - 1 && text[i] == '=')
246  {
247  i++;
248  int num = text.IndexOf(';', i);
249  if (num > i)
250  {
251  m_CharacterSet = contentType.Substring(i, num - i).Trim();
252  }
253  else
254  {
255  m_CharacterSet = contentType.Substring(i).Trim();
256  }
257  break;
258  }
259  }
260  }
261  }
262  return m_CharacterSet;
263  }
264  }
265 
269  public string Server
270  {
271  get
272  {
273  CheckDisposed();
274  string server = m_HttpResponseHeaders.Server;
275  if (server != null)
276  {
277  return server;
278  }
279  return string.Empty;
280  }
281  }
282 
286  public DateTime LastModified
287  {
288  get
289  {
290  CheckDisposed();
291  string lastModified = m_HttpResponseHeaders.LastModified;
292  if (lastModified == null)
293  {
294  return DateTime.Now;
295  }
296  return HttpProtocolUtils.string2date(lastModified);
297  }
298  }
299 
303  [global::__DynamicallyInvokable]
304  public virtual HttpStatusCode StatusCode
305  {
306  [global::__DynamicallyInvokable]
307  get
308  {
309  CheckDisposed();
310  return m_StatusCode;
311  }
312  }
313 
317  [global::__DynamicallyInvokable]
318  public virtual string StatusDescription
319  {
320  [global::__DynamicallyInvokable]
321  get
322  {
323  CheckDisposed();
324  return m_StatusDescription;
325  }
326  }
327 
331  public Version ProtocolVersion
332  {
333  get
334  {
335  CheckDisposed();
336  if (!m_IsVersionHttp11)
337  {
338  return HttpVersion.Version10;
339  }
340  return HttpVersion.Version11;
341  }
342  }
343 
344  internal bool KeepAlive
345  {
346  get
347  {
348  if (m_UsesProxySemantics)
349  {
350  string text = Headers["Proxy-Connection"];
351  if (text != null)
352  {
353  if (text.ToLower(CultureInfo.InvariantCulture).IndexOf("close") >= 0)
354  {
355  return text.ToLower(CultureInfo.InvariantCulture).IndexOf("keep-alive") >= 0;
356  }
357  return true;
358  }
359  }
360  string text2 = Headers["Connection"];
361  if (text2 != null)
362  {
363  text2 = text2.ToLower(CultureInfo.InvariantCulture);
364  }
365  if (ProtocolVersion == HttpVersion.Version10)
366  {
367  if (text2 != null)
368  {
369  return text2.IndexOf("keep-alive") >= 0;
370  }
371  return false;
372  }
373  if (ProtocolVersion >= HttpVersion.Version11)
374  {
375  if (text2 != null && text2.IndexOf("close") >= 0)
376  {
377  return text2.IndexOf("keep-alive") >= 0;
378  }
379  return true;
380  }
381  return false;
382  }
383  }
384 
388  [global::__DynamicallyInvokable]
389  public override Uri ResponseUri
390  {
391  [global::__DynamicallyInvokable]
392  get
393  {
394  CheckDisposed();
395  return m_Uri;
396  }
397  }
398 
402  [global::__DynamicallyInvokable]
403  public virtual string Method
404  {
405  [global::__DynamicallyInvokable]
406  get
407  {
408  CheckDisposed();
409  return m_Verb.Name;
410  }
411  }
412 
417  [global::__DynamicallyInvokable]
418  public override Stream GetResponseStream()
419  {
420  if (Logging.On)
421  {
422  Logging.Enter(Logging.Web, this, "GetResponseStream", "");
423  }
424  CheckDisposed();
425  if (Logging.On)
426  {
427  Logging.PrintInfo(Logging.Web, "ContentLength=" + m_ContentLength);
428  }
429  Stream stream;
430  if (m_IsWebSocketResponse && m_StatusCode == HttpStatusCode.SwitchingProtocols)
431  {
432  if (m_WebSocketConnectionStream == null)
433  {
434  ConnectStream connectStream = m_ConnectStream as ConnectStream;
435  m_WebSocketConnectionStream = new WebSocketConnectionStream(connectStream, ConnectionGroupName);
436  }
437  stream = m_WebSocketConnectionStream;
438  }
439  else
440  {
441  stream = m_ConnectStream;
442  }
443  if (Logging.On)
444  {
445  Logging.Exit(Logging.Web, this, "GetResponseStream", stream);
446  }
447  return stream;
448  }
449 
451  public override void Close()
452  {
453  if (Logging.On)
454  {
455  Logging.Enter(Logging.Web, this, "Close", "");
456  }
457  if (!m_disposed)
458  {
459  m_disposed = true;
460  try
461  {
462  Stream connectStream = m_ConnectStream;
463  ICloseEx closeEx = connectStream as ICloseEx;
464  if (closeEx != null)
465  {
466  closeEx.CloseEx(CloseExState.Normal);
467  }
468  else
469  {
470  connectStream?.Close();
471  }
472  }
473  finally
474  {
475  if (IsWebSocketResponse)
476  {
477  ConnectStream connectStream2 = m_ConnectStream as ConnectStream;
478  if (connectStream2 != null && connectStream2.Connection != null)
479  {
480  connectStream2.Connection.ServicePoint.CloseConnectionGroup(ConnectionGroupName);
481  }
482  }
483  }
484  }
485  if (Logging.On)
486  {
487  Logging.Exit(Logging.Web, this, "Close", "");
488  }
489  }
490 
491  internal void Abort()
492  {
493  Stream connectStream = m_ConnectStream;
494  ICloseEx closeEx = connectStream as ICloseEx;
495  try
496  {
497  if (closeEx != null)
498  {
499  closeEx.CloseEx(CloseExState.Abort);
500  }
501  else
502  {
503  connectStream?.Close();
504  }
505  }
506  catch
507  {
508  }
509  }
510 
514  [global::__DynamicallyInvokable]
515  protected override void Dispose(bool disposing)
516  {
517  if (disposing)
518  {
519  base.Dispose(disposing: true);
520  m_propertiesDisposed = true;
521  }
522  }
523 
525  [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)]
526  [EditorBrowsable(EditorBrowsableState.Never)]
528  {
529  }
530 
531  internal HttpWebResponse(Uri responseUri, KnownHttpVerb verb, CoreResponseData coreData, string mediaType, bool usesProxySemantics, DecompressionMethods decompressionMethod, bool isWebSocketResponse, string connectionGroupName)
532  {
533  m_Uri = responseUri;
534  m_Verb = verb;
535  m_MediaType = mediaType;
536  m_UsesProxySemantics = usesProxySemantics;
537  m_CoreResponseData = coreData;
538  m_ConnectStream = coreData.m_ConnectStream;
539  m_HttpResponseHeaders = coreData.m_ResponseHeaders;
540  m_ContentLength = coreData.m_ContentLength;
541  m_StatusCode = coreData.m_StatusCode;
542  m_StatusDescription = coreData.m_StatusDescription;
543  m_IsVersionHttp11 = coreData.m_IsVersionHttp11;
544  m_IsWebSocketResponse = isWebSocketResponse;
545  m_ConnectionGroupName = connectionGroupName;
546  if (m_ContentLength == 0L && m_ConnectStream is ConnectStream)
547  {
548  ((ConnectStream)m_ConnectStream).CallDone();
549  }
550  string text = m_HttpResponseHeaders["Content-Location"];
551  if (text != null)
552  {
553  try
554  {
555  m_Uri = new Uri(m_Uri, text);
556  }
557  catch (UriFormatException)
558  {
559  }
560  }
561  if (decompressionMethod == DecompressionMethods.None)
562  {
563  return;
564  }
565  string text2 = m_HttpResponseHeaders["Content-Encoding"];
566  if (text2 != null)
567  {
568  if ((decompressionMethod & DecompressionMethods.GZip) != 0 && text2.IndexOf("gzip", StringComparison.CurrentCulture) != -1)
569  {
570  m_ConnectStream = new GZipWrapperStream(m_ConnectStream, CompressionMode.Decompress);
571  m_ContentLength = -1L;
572  m_HttpResponseHeaders["Content-Encoding"] = null;
573  }
574  else if ((decompressionMethod & DecompressionMethods.Deflate) != 0 && text2.IndexOf("deflate", StringComparison.CurrentCulture) != -1)
575  {
576  m_ConnectStream = new DeflateWrapperStream(m_ConnectStream, CompressionMode.Decompress);
577  m_ContentLength = -1L;
578  m_HttpResponseHeaders["Content-Encoding"] = null;
579  }
580  }
581  }
582 
586  [Obsolete("Serialization is obsoleted for this type. http://go.microsoft.com/fwlink/?linkid=14202")]
587  protected HttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
588  : base(serializationInfo, streamingContext)
589  {
590  m_HttpResponseHeaders = (WebHeaderCollection)serializationInfo.GetValue("m_HttpResponseHeaders", typeof(WebHeaderCollection));
591  m_Uri = (Uri)serializationInfo.GetValue("m_Uri", typeof(Uri));
592  m_Certificate = (X509Certificate)serializationInfo.GetValue("m_Certificate", typeof(X509Certificate));
593  Version version = (Version)serializationInfo.GetValue("m_Version", typeof(Version));
594  m_IsVersionHttp11 = version.Equals(HttpVersion.Version11);
595  m_StatusCode = (HttpStatusCode)serializationInfo.GetInt32("m_StatusCode");
596  m_ContentLength = serializationInfo.GetInt64("m_ContentLength");
597  m_Verb = KnownHttpVerb.Parse(serializationInfo.GetString("m_Verb"));
598  m_StatusDescription = serializationInfo.GetString("m_StatusDescription");
599  m_MediaType = serializationInfo.GetString("m_MediaType");
600  }
601 
605  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter, SerializationFormatter = true)]
606  void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
607  {
608  GetObjectData(serializationInfo, streamingContext);
609  }
610 
614  [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
615  protected override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
616  {
617  serializationInfo.AddValue("m_HttpResponseHeaders", m_HttpResponseHeaders, typeof(WebHeaderCollection));
618  serializationInfo.AddValue("m_Uri", m_Uri, typeof(Uri));
619  serializationInfo.AddValue("m_Certificate", m_Certificate, typeof(X509Certificate));
620  serializationInfo.AddValue("m_Version", ProtocolVersion, typeof(Version));
621  serializationInfo.AddValue("m_StatusCode", m_StatusCode);
622  serializationInfo.AddValue("m_ContentLength", m_ContentLength);
623  serializationInfo.AddValue("m_Verb", m_Verb.Name);
624  serializationInfo.AddValue("m_StatusDescription", m_StatusDescription);
625  serializationInfo.AddValue("m_MediaType", m_MediaType);
626  base.GetObjectData(serializationInfo, streamingContext);
627  }
628 
633  public string GetResponseHeader(string headerName)
634  {
635  CheckDisposed();
636  string text = m_HttpResponseHeaders[headerName];
637  if (text != null)
638  {
639  return text;
640  }
641  return string.Empty;
642  }
643 
644  private void CheckDisposed()
645  {
646  if (m_propertiesDisposed)
647  {
648  throw new ObjectDisposedException(GetType().FullName);
649  }
650  }
651  }
652 }
override bool SupportsHeaders
Gets a value that indicates if headers are supported.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
virtual string Method
Gets the method that is used to return the response.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Net.HttpWebResponse, and optionally disposes of...
Describes a set of security permissions applied to code. This class cannot be inherited.
static readonly Version Version11
Defines a T:System.Version instance for HTTP 1.1.
Definition: HttpVersion.cs:10
long GetInt64(string name)
Retrieves a 64-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
DecompressionMethods
Represents the file compression and decompression encoding format to be used to compress the data rec...
override Uri ResponseUri
Gets the URI of the Internet resource that responded to the request.
HttpWebResponse()
Initializes a new instance of the T:System.Net.HttpWebResponse class.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
string ContentEncoding
Gets the method that is used to encode the body of the response.
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
Definition: __Canon.cs:3
override bool Equals(object obj)
Returns a value indicating whether the current T:System.Version object is equal to a specified object...
Definition: Version.cs:390
override void Close()
Closes the response stream.
CompressionMode
Specifies whether to compress or decompress the underlying stream.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
Defines the HTTP version numbers that are supported by the T:System.Net.HttpWebRequest and T:System....
Definition: HttpVersion.cs:4
HttpStatusCode
Contains the values of status codes defined for HTTP.
string CharacterSet
Gets the character set of the response.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
DateTime LastModified
Gets the last date and time that the contents of the response were modified.
Contains protocol headers associated with a request or response.
override long ContentLength
Gets the length of the content returned by the request.
The exception that is thrown when an operation is performed on a disposed object.
SecurityAction
Specifies the security actions that can be performed using declarative security.
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Definition: Stream.cs:855
Provides a response from a Uniform Resource Identifier (URI). This is an abstract class.
Definition: WebResponse.cs:10
void AddValue(string name, object value, Type type)
Adds a value into the T:System.Runtime.Serialization.SerializationInfo store, where value is associa...
static readonly Version Version10
Defines a T:System.Version instance for HTTP 1.0.
Definition: HttpVersion.cs:7
Version ProtocolVersion
Gets the version of the HTTP protocol that is used in the response.
override WebHeaderCollection Headers
Gets the headers that are associated with this response from the server.
override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
override string ContentType
Gets the content type of the response.
string GetResponseHeader(string headerName)
Gets the contents of a header that was returned with the response.
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
virtual HttpStatusCode StatusCode
Gets the status of the response.
Provides an HTTP-specific implementation of the T:System.Net.WebResponse class.
virtual string StatusDescription
Gets the status description returned with the response.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
string Server
Gets the name of the server that sent the response.
string GetString(string name)
Retrieves a T:System.String value from the T:System.Runtime.Serialization.SerializationInfo store.
object GetValue(string name, Type type)
Retrieves a value from the T:System.Runtime.Serialization.SerializationInfo store.
Specifies that the class can be serialized.
static DateTime Now
Gets a T:System.DateTime object that is set to the current date and time on this computer,...
Definition: DateTime.cs:264
int GetInt32(string name)
Retrieves a 32-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
virtual CookieCollection Cookies
Gets or sets the cookies that are associated with this response.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
HttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
Initializes a new instance of the T:System.Net.HttpWebResponse class from the specified T:System....
override bool IsMutuallyAuthenticated
Gets a T:System.Boolean value that indicates whether both client and server were authenticated.
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Provides methods that help you use X.509 v.3 certificates.
override Stream GetResponseStream()
Gets the stream that is used to read the body of the response from the server.
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16