《C# 爬虫 破境之道》:第一境 爬虫原理 — 第三节:WebResponse
第二节中,我们介绍了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# 破境之道】
方便各位在有疑问的时候可以及时给我个反馈。同时,也算是给各位志同道合的朋友提供一个交流的平台。
需要源码的童鞋,也可以在群文件中获取最新源代码。
《C# 爬虫 破境之道》:第一境 爬虫原理 — 第三节:WebResponse的更多相关文章
- 网络爬虫入门:你的第一个爬虫项目(requests库)
		
0.采用requests库 虽然urllib库应用也很广泛,而且作为Python自带的库无需安装,但是大部分的现在python爬虫都应用requests库来处理复杂的http请求.requests库语 ...
 - Python爬虫实践 -- 记录我的第一只爬虫
		
一.环境配置 1. 下载安装 python3 .(或者安装 Anaconda) 2. 安装requests和lxml 进入到 pip 目录,CMD --> C:\Python\Scripts,输 ...
 - 《C# 爬虫 破境之道》:第二境 爬虫应用 — 第一节:HTTP协议数据采集
		
首先欢迎您来到本书的第二境,本境,我们将全力打造一个实际生产环境可用的爬虫应用了.虽然只是刚开始,虽然路漫漫其修远,不过还是有点小鸡冻:P 本境打算针对几大派生类做进一步深耕,包括与应用的结合.对比它 ...
 - 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第六节:第一境尾声
		
在第一境中,我们主要了解了爬虫的一些基本原理,说原理也行,说基础知识也罢,结果就是已经知道一个小爬虫是如何诞生的了~那么现在,请默默回想一下,在第一境中,您都掌握了哪些内容?哪些还比较模糊?如果还有什 ...
 - 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第五节:数据流处理的那些事儿
		
为什么说到数据流了呢,因为上一节中介绍了一下异步发送请求.同样,在数据流的处理上,C#也为我们提供几个有用的异步处理方法.而且,爬虫这生物,处理数据流是基础本能,比较重要.本着这个原则,就聊一聊吧. ...
 - 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第二节:WebRequest
		
本节主要来介绍一下,在C#中制造爬虫,最为常见.常用.实用的基础类 ------ WebRequest.WebResponse. 先来看一个示例 [1.2.1]: using System; usin ...
 - 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第一节:整体思路
		
在构建本章节内容的时候,笔者也在想一个问题,究竟什么样的采集器框架,才能算得上是一个“全能”的呢?就我自己以往项目经历而言,可以归纳以下几个大的分类: 根据通讯协议:HTTP的.HTTPS的.TCP的 ...
 - 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第四节:同步与异步请求方式
		
前两节,我们对WebRequest和WebResponse这两个类做了介绍,但两者还相对独立.本节,我们来说说如何将两者结合起来,方式有哪些,有什么不同. 1.4.1 说结合,无非就是我们如何发送一个 ...
 - 《C# 爬虫 破境之道》:概述
		
第一节:写作本书的目的 关于笔者 张晓亭(Mike Cheers),1982年出生,内蒙古辽阔的大草原是我的故乡. 没有高学历,没有侃侃而谈的高谈阔论,拥有的就是那一份对技术的执著,对自我价值的追求. ...
 
随机推荐
- SpringSide 3 中的安全框架
			
在SpringSide 3的官方文档中,说安全框架使用的是Spring Security 2.0.乍一看,吓了我一跳,以为Acegi这么快就被淘汰了呢.上搜索引擎一搜,发现原来Spring Secur ...
 - 2018-4-29-C#-将dll打包到程序中
			
title author date CreateTime categories C# 将dll打包到程序中 lindexi 2018-04-29 09:43:22 +0800 2018-2-13 17 ...
 - P1036 最大公约数
			
题目描述 给你两个正整数A和B,求它们的最大公约数. 输入格式 两个正整数 \(A,B(1 \le A,B \le 10^9)\) . 输出格式 一个整数,表示A和B的最大公约数. 样例输入 6 8 ...
 - SQLite3的使用(封装很长,直接使用sqlite3_open函数,LIBS += sqlite3.dll 即可)good
			
1.下载sqlite3相关文件sqlite3.dll.sqlite3.h(可从http://download.csdn.net/detail/mingxia_sui/5249070下载),添加到工程的 ...
 - 一键自动生成 java junit 测试代码神器 gen-test-plugin 入门介绍
			
gen-test-plugin 我们日常编写代码的过程中,经常需要为代码编写测试案例. 随着对代码质量的要求越来越高,很多公司开始通过代码的测试覆盖率作为 QA 的一个评定指标. 本框架可以一键生成所 ...
 - Python深层拷贝
			
import copy new_instance = copy.deepcopy(instance)
 - 第二阶段:4.商业需求文档MRD:2.PRD-功能详细说明
			
功能详细说明 功能一般分两类:1.通用性功能(导航,菜单项) 通过PRD让各个部门清楚的了解产品的各项功能构成.
 - mac如何查看已连接wifi的密码
			
可以通道mac自带的“钥匙串访问”功能查看.选择需要查询的wifi名称,右击选择“将密码拷贝到剪贴板”,输入管理员密码后,密码就拷贝好了. 找个地方粘贴即可看到密码
 - 为什么IIS应用程序池回收时间默认被设置为1740分钟?
			
作者:斯科特 福赛斯/Scott Forsyth日期:2013/04/06地址:http://weblogs.asp.net/owscott/why-is-the-iis-default-app-po ...
 - Alibaba Cloud Toolkit 使用心得(IDEA版)
			
一.安装插件 确保 IntelliJ IDEA 在 2018.1 或更高版本 打开 Settings - Plugins 搜索安装 Alibaba Cloud Toolkit 二.配置环境 Deplo ...