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

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

[Code 1.3.1]

  1. //
  2. // Summary:
  3. // Provides a response from a Uniform Resource Identifier (URI). This is an abstract
  4. // class.
  5. public abstract class WebResponse : MarshalByRefObject, ISerializable, IDisposable
  6. {
  7. //
  8. // Summary:
  9. // Initializes a new instance of the System.Net.WebResponse class.
  10. protected WebResponse();
  11. //
  12. // Summary:
  13. // Initializes a new instance of the System.Net.WebResponse class from the specified
  14. // instances of the System.Runtime.Serialization.SerializationInfo and System.Runtime.Serialization.StreamingContext
  15. // classes.
  16. //
  17. // Parameters:
  18. // serializationInfo:
  19. // An instance of the System.Runtime.Serialization.SerializationInfo class that
  20. // contains the information required to serialize the new System.Net.WebRequest
  21. // instance.
  22. //
  23. // streamingContext:
  24. // An instance of the System.Runtime.Serialization.StreamingContext class that indicates
  25. // the source of the serialized stream that is associated with the new System.Net.WebRequest
  26. // instance.
  27. //
  28. // Exceptions:
  29. // T:System.NotSupportedException:
  30. // Any attempt is made to access the constructor, when the constructor is not overridden
  31. // in a descendant class.
  32. protected WebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext);
  33.  
  34. //
  35. // Summary:
  36. // Gets a System.Boolean value that indicates whether this response was obtained
  37. // from the cache.
  38. //
  39. // Returns:
  40. // true if the response was taken from the cache; otherwise, false.
  41. public virtual bool IsFromCache { get; }
  42. //
  43. // Summary:
  44. // Gets a System.Boolean value that indicates whether mutual authentication occurred.
  45. //
  46. // Returns:
  47. // true if both client and server were authenticated; otherwise, false.
  48. public virtual bool IsMutuallyAuthenticated { get; }
  49. //
  50. // Summary:
  51. // When overridden in a descendant class, gets or sets the content length of data
  52. // being received.
  53. //
  54. // Returns:
  55. // The number of bytes returned from the Internet resource.
  56. //
  57. // Exceptions:
  58. // T:System.NotSupportedException:
  59. // Any attempt is made to get or set the property, when the property is not overridden
  60. // in a descendant class.
  61. public virtual long ContentLength { get; set; }
  62. //
  63. // Summary:
  64. // When overridden in a derived class, gets or sets the content type of the data
  65. // being received.
  66. //
  67. // Returns:
  68. // A string that contains the content type of the response.
  69. //
  70. // Exceptions:
  71. // T:System.NotSupportedException:
  72. // Any attempt is made to get or set the property, when the property is not overridden
  73. // in a descendant class.
  74. public virtual string ContentType { get; set; }
  75. //
  76. // Summary:
  77. // When overridden in a derived class, gets the URI of the Internet resource that
  78. // actually responded to the request.
  79. //
  80. // Returns:
  81. // An instance of the System.Uri class that contains the URI of the Internet resource
  82. // that actually responded to the request.
  83. //
  84. // Exceptions:
  85. // T:System.NotSupportedException:
  86. // Any attempt is made to get or set the property, when the property is not overridden
  87. // in a descendant class.
  88. public virtual Uri ResponseUri { get; }
  89. //
  90. // Summary:
  91. // When overridden in a derived class, gets a collection of header name-value pairs
  92. // associated with this request.
  93. //
  94. // Returns:
  95. // An instance of the System.Net.WebHeaderCollection class that contains header
  96. // values associated with this response.
  97. //
  98. // Exceptions:
  99. // T:System.NotSupportedException:
  100. // Any attempt is made to get or set the property, when the property is not overridden
  101. // in a descendant class.
  102. public virtual WebHeaderCollection Headers { get; }
  103. //
  104. // Summary:
  105. // Gets a value that indicates if headers are supported.
  106. //
  107. // Returns:
  108. // Returns System.Boolean. true if headers are supported; otherwise, false.
  109. public virtual bool SupportsHeaders { get; }
  110.  
  111. //
  112. // Summary:
  113. // When overridden by a descendant class, closes the response stream.
  114. //
  115. // Exceptions:
  116. // T:System.NotSupportedException:
  117. // Any attempt is made to access the method, when the method is not overridden in
  118. // a descendant class.
  119. public virtual void Close();
  120. //
  121. // Summary:
  122. // Releases the unmanaged resources used by the System.Net.WebResponse object.
  123. public void Dispose();
  124. //
  125. // Summary:
  126. // When overridden in a descendant class, returns the data stream from the Internet
  127. // resource.
  128. //
  129. // Returns:
  130. // An instance of the System.IO.Stream class for reading data from the Internet
  131. // resource.
  132. //
  133. // Exceptions:
  134. // T:System.NotSupportedException:
  135. // Any attempt is made to access the method, when the method is not overridden in
  136. // a descendant class.
  137. public virtual Stream GetResponseStream();
  138. //
  139. // Summary:
  140. // Releases the unmanaged resources used by the System.Net.WebResponse object, and
  141. // optionally disposes of the managed resources.
  142. //
  143. // Parameters:
  144. // disposing:
  145. // true to release both managed and unmanaged resources; false to releases only
  146. // unmanaged resources.
  147. protected virtual void Dispose(bool disposing);
  148. //
  149. // Summary:
  150. // Populates a System.Runtime.Serialization.SerializationInfo with the data that
  151. // is needed to serialize the target object.
  152. //
  153. // Parameters:
  154. // serializationInfo:
  155. // The System.Runtime.Serialization.SerializationInfo to populate with data.
  156. //
  157. // streamingContext:
  158. // A System.Runtime.Serialization.StreamingContext that specifies the destination
  159. // for this serialization.
  160. protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext);
  161. }

System.Net.WebResponse 全貌

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

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

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

几个属性

[Code 1.3.2]

  1. /// <summary>
  2. /// 只读,指示这个回复是不是从缓存中获取的
  3. /// </summary>
  4. public virtual bool IsFromCache { get; }
  5. /// <summary>
  6. /// 只读,指示是否发生相互认证
  7. /// </summary>
  8. public virtual bool IsMutuallyAuthenticated { get; }
  9. /// <summary>
  10. /// 指示收到的数据长度
  11. /// </summary>
  12. public virtual long ContentLength { get; set; }
  13. /// <summary>
  14. /// 指示收到的内容类型
  15. /// </summary>
  16. public virtual string ContentType { get; set; }
  17. /// <summary>
  18. /// 只读,指示收到的实际资源URI
  19. /// </summary>
  20. public virtual Uri ResponseUri { get; }
  21. /// <summary>
  22. /// 只读,Header集合,HTTP协议中的重点
  23. /// </summary>
  24. public virtual WebHeaderCollection Headers { get; }
  25. /// <summary>
  26. /// 只读,指示是否支持Header集合
  27. /// </summary>
  28. public virtual bool SupportsHeaders { get; }

System.Net.WebResponse 属性

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

几个方法

[Code 1.3.3]

  1. /// <summary>
  2. /// 获得目标资源的数据流
  3. /// </summary>
  4. public virtual Stream GetResponseStream();
  5. /// <summary>
  6. /// 关闭Response流
  7. /// </summary>
  8. public virtual void Close();
  9. /// <summary>
  10. /// 释放WebResponse对象
  11. /// </summary>
  12. public void Dispose();
  13. /// <summary>
  14. /// 释放WebResponse对象
  15. /// </summary>
  16. protected virtual void Dispose(bool disposing);
  17. /// <summary>
  18. /// 使用序列化目标对象所需的数据填充System.Runtime.Serialization.SerializationInfo
  19. /// </summary>
  20. protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext);

System.Net.WebResponse 方法

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

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

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

《C# 爬虫 破境之道》:第一境 爬虫原理 — 第三节:WebResponse的更多相关文章

  1. 网络爬虫入门:你的第一个爬虫项目(requests库)

    0.采用requests库 虽然urllib库应用也很广泛,而且作为Python自带的库无需安装,但是大部分的现在python爬虫都应用requests库来处理复杂的http请求.requests库语 ...

  2. Python爬虫实践 -- 记录我的第一只爬虫

    一.环境配置 1. 下载安装 python3 .(或者安装 Anaconda) 2. 安装requests和lxml 进入到 pip 目录,CMD --> C:\Python\Scripts,输 ...

  3. 《C# 爬虫 破境之道》:第二境 爬虫应用 — 第一节:HTTP协议数据采集

    首先欢迎您来到本书的第二境,本境,我们将全力打造一个实际生产环境可用的爬虫应用了.虽然只是刚开始,虽然路漫漫其修远,不过还是有点小鸡冻:P 本境打算针对几大派生类做进一步深耕,包括与应用的结合.对比它 ...

  4. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第六节:第一境尾声

    在第一境中,我们主要了解了爬虫的一些基本原理,说原理也行,说基础知识也罢,结果就是已经知道一个小爬虫是如何诞生的了~那么现在,请默默回想一下,在第一境中,您都掌握了哪些内容?哪些还比较模糊?如果还有什 ...

  5. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第五节:数据流处理的那些事儿

    为什么说到数据流了呢,因为上一节中介绍了一下异步发送请求.同样,在数据流的处理上,C#也为我们提供几个有用的异步处理方法.而且,爬虫这生物,处理数据流是基础本能,比较重要.本着这个原则,就聊一聊吧. ...

  6. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第二节:WebRequest

    本节主要来介绍一下,在C#中制造爬虫,最为常见.常用.实用的基础类 ------ WebRequest.WebResponse. 先来看一个示例 [1.2.1]: using System; usin ...

  7. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第一节:整体思路

    在构建本章节内容的时候,笔者也在想一个问题,究竟什么样的采集器框架,才能算得上是一个“全能”的呢?就我自己以往项目经历而言,可以归纳以下几个大的分类: 根据通讯协议:HTTP的.HTTPS的.TCP的 ...

  8. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第四节:同步与异步请求方式

    前两节,我们对WebRequest和WebResponse这两个类做了介绍,但两者还相对独立.本节,我们来说说如何将两者结合起来,方式有哪些,有什么不同. 1.4.1 说结合,无非就是我们如何发送一个 ...

  9. 《C# 爬虫 破境之道》:概述

    第一节:写作本书的目的 关于笔者 张晓亭(Mike Cheers),1982年出生,内蒙古辽阔的大草原是我的故乡. 没有高学历,没有侃侃而谈的高谈阔论,拥有的就是那一份对技术的执著,对自我价值的追求. ...

随机推荐

  1. H3C 显示OSPF的链路状态数据库

  2. Total Commander 显示文件包含文件名扩展

    在默认的 Total Commander 是分开文件名和文件扩展,如果想要让文件名同时显示扩展,可以通过设置合并文件名和扩展两列 点击配置,在选项的制表修改显示 How to configure To ...

  3. asp dotnet core 图片在浏览器没访问可能原因

    我写了一个项目用来广告就用到广告的图片,但是广告的图片放在博客的链接无法访问,连我的方法都没有调用,而我尝试网页直接访问图片链接是可以访问的,最后找到原因是广告插件禁用了图片访问 我在一个方法创建了广 ...

  4. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  5. 关于vue-cli打包配置部署404

    在vue脚手架(vue-cli)下我很很快的就可以搭建自己的开发环境,但是我们把项目编写完后,需要进行打包上线会遇到各种问题,在根据版本问题,(vue3的版本跟之前相比少了很多配置项),下面是我用老版 ...

  6. Kobjects, Ksets 和 Subsystems

    Kobject 是基础的结构, 它保持设备模型在一起. 初始地它被作为一个简单的引用计数, 但是它的责任已随时间增长, 并且因此有了它自己的战场. struct kobject 所处理的任 务和它的支 ...

  7. 2019-8-31-C#-匹配可空变量

    title author date CreateTime categories C# 匹配可空变量 lindexi 2019-08-31 16:55:58 +0800 2019-06-01 08:40 ...

  8. swiper 使用参考 禁止手动滑动 监听事件

    最外层容器加类名  swiper-no-swiping 监听切换事件 onTransitionEnd: function(swiper){ console.log('过渡结束'); }

  9. Qt4.5 QFrame(相当于Delphi里的TPanel,有各种凹凸方式)

    QFrame类是有框架的窗口部件的基类. QPopupMenu使用这个来把菜单“升高”,高于周围屏幕.QProgressBar有“凹陷”的外观.QLabel有平坦的外观.这些有框架的窗口部件可以被改变 ...

  10. MFC入门

    目录 001.MFC_应用程序类型    002.MFC_对话框_静态文本_编辑框  003.MFC_对话框_访问控件_7种方法_A   004.MFC_对话框_访问控件_7种方法_B   005.M ...