第二节中,我们介绍了WebRequest,它可以帮助我们发送一个请求,不过正所谓“来而不往非礼也”,对方收到我们的请求,不给点回复,貌似不太合适(不过,还真有脸皮厚的:P)。

接下来,就重点研究一下,我们收到的回复,是个什么样的东东

[Code 1.3.1]

     //
// Summary:
// Provides a response from a Uniform Resource Identifier (URI). This is an abstract
// class.
public abstract class WebResponse : MarshalByRefObject, ISerializable, IDisposable
{
//
// Summary:
// Initializes a new instance of the System.Net.WebResponse class.
protected WebResponse();
//
// Summary:
// Initializes a new instance of the System.Net.WebResponse class from the specified
// instances of the System.Runtime.Serialization.SerializationInfo and System.Runtime.Serialization.StreamingContext
// classes.
//
// Parameters:
// serializationInfo:
// An instance of the System.Runtime.Serialization.SerializationInfo class that
// contains the information required to serialize the new System.Net.WebRequest
// instance.
//
// streamingContext:
// An instance of the System.Runtime.Serialization.StreamingContext class that indicates
// the source of the serialized stream that is associated with the new System.Net.WebRequest
// instance.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to access the constructor, when the constructor is not overridden
// in a descendant class.
protected WebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext); //
// Summary:
// Gets a System.Boolean value that indicates whether this response was obtained
// from the cache.
//
// Returns:
// true if the response was taken from the cache; otherwise, false.
public virtual bool IsFromCache { get; }
//
// Summary:
// Gets a System.Boolean value that indicates whether mutual authentication occurred.
//
// Returns:
// true if both client and server were authenticated; otherwise, false.
public virtual bool IsMutuallyAuthenticated { get; }
//
// Summary:
// When overridden in a descendant class, gets or sets the content length of data
// being received.
//
// Returns:
// The number of bytes returned from the Internet resource.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to get or set the property, when the property is not overridden
// in a descendant class.
public virtual long ContentLength { get; set; }
//
// Summary:
// When overridden in a derived class, gets or sets the content type of the data
// being received.
//
// Returns:
// A string that contains the content type of the response.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to get or set the property, when the property is not overridden
// in a descendant class.
public virtual string ContentType { get; set; }
//
// Summary:
// When overridden in a derived class, gets the URI of the Internet resource that
// actually responded to the request.
//
// Returns:
// An instance of the System.Uri class that contains the URI of the Internet resource
// that actually responded to the request.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to get or set the property, when the property is not overridden
// in a descendant class.
public virtual Uri ResponseUri { get; }
//
// Summary:
// When overridden in a derived class, gets a collection of header name-value pairs
// associated with this request.
//
// Returns:
// An instance of the System.Net.WebHeaderCollection class that contains header
// values associated with this response.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to get or set the property, when the property is not overridden
// in a descendant class.
public virtual WebHeaderCollection Headers { get; }
//
// Summary:
// Gets a value that indicates if headers are supported.
//
// Returns:
// Returns System.Boolean. true if headers are supported; otherwise, false.
public virtual bool SupportsHeaders { get; } //
// Summary:
// When overridden by a descendant class, closes the response stream.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to access the method, when the method is not overridden in
// a descendant class.
public virtual void Close();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.WebResponse object.
public void Dispose();
//
// Summary:
// When overridden in a descendant class, returns the data stream from the Internet
// resource.
//
// Returns:
// An instance of the System.IO.Stream class for reading data from the Internet
// resource.
//
// Exceptions:
// T:System.NotSupportedException:
// Any attempt is made to access the method, when the method is not overridden in
// a descendant class.
public virtual Stream GetResponseStream();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.WebResponse object, and
// optionally disposes of the managed resources.
//
// Parameters:
// disposing:
// true to release both managed and unmanaged resources; false to releases only
// unmanaged resources.
protected virtual void Dispose(bool disposing);
//
// Summary:
// Populates a System.Runtime.Serialization.SerializationInfo with the data that
// is needed to serialize the target object.
//
// Parameters:
// serializationInfo:
// The System.Runtime.Serialization.SerializationInfo to populate with data.
//
// streamingContext:
// A System.Runtime.Serialization.StreamingContext that specifies the destination
// for this serialization.
protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext);
}

System.Net.WebResponse 全貌

相比较WebRequest而言,WebResponse简单许多,这里就不再分解片断讲解了。有事儿说事儿,不要以貌取人:D

首先,与WebRequest的类似,它还是抽象类,两个构造函数的结构,一票子虚属性,大部分都是只读的,和一票子虚方法。

其次,与WebRequest不同,它继承了System.IDisposable接口,提醒我们啊,用完了,要释放啊~~~~~~

几个属性

[Code 1.3.2]

         /// <summary>
/// 只读,指示这个回复是不是从缓存中获取的
/// </summary>
public virtual bool IsFromCache { get; }
/// <summary>
/// 只读,指示是否发生相互认证
/// </summary>
public virtual bool IsMutuallyAuthenticated { get; }
/// <summary>
/// 指示收到的数据长度
/// </summary>
public virtual long ContentLength { get; set; }
/// <summary>
/// 指示收到的内容类型
/// </summary>
public virtual string ContentType { get; set; }
/// <summary>
/// 只读,指示收到的实际资源URI
/// </summary>
public virtual Uri ResponseUri { get; }
/// <summary>
/// 只读,Header集合,HTTP协议中的重点
/// </summary>
public virtual WebHeaderCollection Headers { get; }
/// <summary>
/// 只读,指示是否支持Header集合
/// </summary>
public virtual bool SupportsHeaders { get; }

System.Net.WebResponse 属性

重要的属性有Headers、ContentType、ContentLength。对于分析数据都是举足轻重的。

几个方法

[Code 1.3.3]

         /// <summary>
/// 获得目标资源的数据流
/// </summary>
public virtual Stream GetResponseStream();
/// <summary>
/// 关闭Response流
/// </summary>
public virtual void Close();
/// <summary>
/// 释放WebResponse对象
/// </summary>
public void Dispose();
/// <summary>
/// 释放WebResponse对象
/// </summary>
protected virtual void Dispose(bool disposing);
/// <summary>
/// 使用序列化目标对象所需的数据填充System.Runtime.Serialization.SerializationInfo
/// </summary>
protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext);

System.Net.WebResponse 方法

重要的方法有GetResponseStream、Close。对于获取数据至关重要。

总而言之,WebResponse还是比较简单的,给我们留下的坑不多。与WebRequest的讲解一样,干嚼无味,还是在实例中体会个中滋味吧:P

喜欢本系列丛书的朋友,可以点击链接加入QQ交流群(994761602)【C# 破境之道】
方便各位在有疑问的时候可以及时给我个反馈。同时,也算是给各位志同道合的朋友提供一个交流的平台。
需要源码的童鞋,也可以在群文件中获取最新源代码。

最新文章

  1. 使用javascript实现html页面直接下载网盘文件
  2. SQL Server索引进阶第五篇:索引包含列 .
  3. AlarmManager使用注意事项
  4. 完整的Android手机短信验证源码
  5. 不合法语句 self.contentView.frame.origin.x = x;
  6. 【转载】将绿色版Tomcat服务添加到系统服务并设为开机运行
  7. metasploit联动beef启动
  8. 用css以写代码形式画一个皮卡丘
  9. nyoj 开方数
  10. .net core2.1 使用 dynamic 类型报错
  11. sqlmap的安装
  12. Flask 框架 debug=Ture 和Json解码:
  13. mysql5.7 pxc
  14. element-ui input输入框回车事件
  15. Vue 需要使用jsonp解决跨域时,可以使用(vue-jsonp)
  16. Python的set集合详解
  17. python 书籍推荐 二
  18. servlet的登陆案例
  19. FPGA论文
  20. Java面向对象_抽象类应用——模板方法模式

热门文章

  1. H5 存储数据sessionStorage
  2. Spring Security 学习笔记-授权控制过滤器
  3. H3C 根据子网数划分子网
  4. H3C STP可选配置
  5. vue中的时间修饰符stop,self
  6. [数论] hdu 5974 A Simple Math Problem (数论gcd)
  7. Codeforces Round #587 C. White Sheet(思维+计算几何)
  8. SQL常见命令
  9. 一键自动生成 java junit 测试代码神器 gen-test-plugin 入门介绍
  10. HolidayFileDisPersonViewList.js中的一些基础