C#如何使用 WebRequest 类请求网页Headers及内容数据源码
2018-03-07 07:37
901 查看
学习目标:C#利用WebRequest 类请求网页Headers及内容数据源码练习平台:Windows10企业版(64位)测试工具:MicrosoftVisual Studio2017(版本默认安装)。程序语言:.NET C#主要内容方法:WebRequest request = WebRequest.Create("http://www.baidu.com/");//调用 Create 来创建 WebRequest 实例。
request.Credentials = CredentialCache.DefaultCredentials;//大多数情况下,WebRequest 类足以接收数据。 但是,如果需要设置特定于协议的属性,必须将 WebRequest 转换为特定于协议的类型。
//例如,要访问 HttpWebRequest 特定于 HTTP 的属性,请将 WebRequest 转换为 HttpWebRequest 引用。
((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
//若要向服务器发送请求,请调用 GetResponse。 返回的 WebResponse 对象的实际类型由请求的 URI 的架构决定。
WebResponse response = request.GetResponse();
//使用完 WebResponse 对象后,必须通过调用 Close 方法将其关闭。 或者,如果已从响应对象获取响应流,可以通过调用 Stream.Close 方法关闭流。 如果不关闭响应或流,应用程序会耗尽与服务器的连接,并变得无法处理其他请求。
//可以访问 WebResponse 的属性或将 WebResponse 转换为特定于协议的实例来读取特定于协议的属性。
//例如,要访问 HttpWebResponse 特定于 HTTP 的属性,请将 WebResponse 转换为 HttpWebResponse 引用。
//以下代码示例演示如何显示与响应一起发送的状态信息。
Console.WriteLine(((HttpWebResponse)response).StatusDescription);//返回内容:OK
listBox1.Items.Add(((HttpWebResponse)response).StatusDescription);
Console.WriteLine(((HttpWebResponse)response).CharacterSet );//返回内容:utf-8
listBox1.Items.Add(((HttpWebResponse)response).CharacterSet);
Console.WriteLine(((HttpWebResponse)response).ContentType );//返回内容:获取响应的内容类型。//text/html; charset=utf-8
listBox1.Items.Add(((HttpWebResponse)response).ContentType);
Console.WriteLine(((HttpWebResponse)response).Headers ); //返回内容:
//Transfer-Encoding: chunked
//Connection: Keep - Alive
//Vary: Accept - Encoding
//Content - Type: text / html; charset = utf - 8
//Date: Tue, 06 Mar 2018 10:10:43 GMT
//Set - Cookie: BAIDUID = 6973B5A9781E0E105D3FA98B215130DD: FG = 1; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,BIDUPSID = 6973B5A9781E0E105D3FA98B215130DD; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,PSTM = 1520331043; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,BDSVRTM = 0; path =/,BD_HOME = 0; path =/,H_PS_PSSID = 1468_21095_22157; path =/; domain =.baidu.com
//P3P: CP = " OTI DSP COR IVA OUR IND COM "
//Cache - Control: private
//Cxy_all: baidu+c66df81753e8860398b1562b8631b3ee
//Expires: Tue, 06 Mar 2018 10:10:17 GMT
//X-Powered-By: HPHP
//Server: BWS/1.1
//X-UA-Compatible: IE=Edge,chrome=1
//BDPAGETYPE: 1
//BDQID: 0xde435e9f000118bf
//BDUSERID: 0
源码下载请稍等。
request.Credentials = CredentialCache.DefaultCredentials;//大多数情况下,WebRequest 类足以接收数据。 但是,如果需要设置特定于协议的属性,必须将 WebRequest 转换为特定于协议的类型。
//例如,要访问 HttpWebRequest 特定于 HTTP 的属性,请将 WebRequest 转换为 HttpWebRequest 引用。
((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
//若要向服务器发送请求,请调用 GetResponse。 返回的 WebResponse 对象的实际类型由请求的 URI 的架构决定。
WebResponse response = request.GetResponse();
//使用完 WebResponse 对象后,必须通过调用 Close 方法将其关闭。 或者,如果已从响应对象获取响应流,可以通过调用 Stream.Close 方法关闭流。 如果不关闭响应或流,应用程序会耗尽与服务器的连接,并变得无法处理其他请求。
//可以访问 WebResponse 的属性或将 WebResponse 转换为特定于协议的实例来读取特定于协议的属性。
//例如,要访问 HttpWebResponse 特定于 HTTP 的属性,请将 WebResponse 转换为 HttpWebResponse 引用。
//以下代码示例演示如何显示与响应一起发送的状态信息。
Console.WriteLine(((HttpWebResponse)response).StatusDescription);//返回内容:OK
listBox1.Items.Add(((HttpWebResponse)response).StatusDescription);
Console.WriteLine(((HttpWebResponse)response).CharacterSet );//返回内容:utf-8
listBox1.Items.Add(((HttpWebResponse)response).CharacterSet);
Console.WriteLine(((HttpWebResponse)response).ContentType );//返回内容:获取响应的内容类型。//text/html; charset=utf-8
listBox1.Items.Add(((HttpWebResponse)response).ContentType);
Console.WriteLine(((HttpWebResponse)response).Headers ); //返回内容:
//Transfer-Encoding: chunked
//Connection: Keep - Alive
//Vary: Accept - Encoding
//Content - Type: text / html; charset = utf - 8
//Date: Tue, 06 Mar 2018 10:10:43 GMT
//Set - Cookie: BAIDUID = 6973B5A9781E0E105D3FA98B215130DD: FG = 1; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,BIDUPSID = 6973B5A9781E0E105D3FA98B215130DD; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,PSTM = 1520331043; expires = Thu, 31 - Dec - 37 23:55:55 GMT; max - age = 2147483647; path =/; domain =.baidu.com,BDSVRTM = 0; path =/,BD_HOME = 0; path =/,H_PS_PSSID = 1468_21095_22157; path =/; domain =.baidu.com
//P3P: CP = " OTI DSP COR IVA OUR IND COM "
//Cache - Control: private
//Cxy_all: baidu+c66df81753e8860398b1562b8631b3ee
//Expires: Tue, 06 Mar 2018 10:10:17 GMT
//X-Powered-By: HPHP
//Server: BWS/1.1
//X-UA-Compatible: IE=Edge,chrome=1
//BDPAGETYPE: 1
//BDQID: 0xde435e9f000118bf
//BDUSERID: 0
源码下载请稍等。
相关文章推荐
- C#如何使用HttpWebRequest、HttpWebResponse模拟浏览器抓取网页内容
- 使用 WebRequest 类请求数据全过程(整理自msdn)
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- 使用HttpWebRequest与HttpWebResponse抓取网页数据
- 如何让HttpWebRequest使用指定网络接口传输数据
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 用C#使用HttpWebRequest Post数据时如何保持Session
- WebClient HttpWebRequest从网页中获取请求数据
- [zt]使用 AJAX 的 Sys.Net.WebRequest 从客户端请求网页
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 用C#使用HttpWebRequest Post数据时如何保持Session
- 使用HttpWebRequest的POST取得网页内容(异步操作)
- 使用HttpWebRequest访问网页及获取数据