1,通过HttpWebRequest、HttpWebResponse获取一个流

                request =  (HttpWebRequest)System.Net.WebRequest.Create(this._url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reciveStream = response.GetResponseStream();

2,读取流成字符串

方法1:利用Stream的Read方法

 byte[] byteData = new byte[response.ContentLength];
int count = byteData.Length, offset = ;
while (count > )
{
int n = reciveStream.Read(byteData, offset, count);
if (n == )
{
break;
}
offset += n;
count -= n;
} string strHtml = System.Text.Encoding.GetEncoding("utf-8").GetString(byteData);
lstURL.Add(strHtml);
response.Close();

方法1中的response.ContentLength可能为-1(和服务器的压缩有关),造成数组初始化失败。

网上方法:添加 request.Headers.Set("Accept-Encoding", "identity"); 强制服务器不压缩,但是我测试的时候总是超时,所以该方法可靠性不定。

建议不要用方法1,用下面的方法2。

方法2:利用StreamReader

using (StreamReader reader = new StreamReader(reciveStream, System.Text.Encoding.UTF8))
{
string strHtml = reader.ReadToEnd(); lstURL.Add(strHtml);
}
response.Close();

注意点:

1,要关闭流。(选一个就可以了)

response.Close() 或 reciveStream.Close()

2,注意编码。

StreamReader默认使用utf-8。
不管是使用stream,还是streamReader,都建议根据具体网页内容,指定编码,不然会出现乱码。

HttpWebRequest、HttpWebResponse获取网页的更多相关文章

  1. asp.net 利用HttpWebRequest自动获取网页编码并获取网页源代码

    /// <summary> /// 获取源代码 /// </summary> /// <param name="url"></param& ...

  2. C#获取网页内容 (WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)

    获取网页数据有很多种方式.在这里主要讲述通过WebClient.WebBrowser和HttpWebRequest/HttpWebResponse三种方式获取网页内容. 这里获取的是包括网页的所有信息 ...

  3. C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)

    一.通过WebClient获取网页内容 这是一种很简单的获取方式,当然,其它的获取方法也很简单.在这里首先要说明的是,如果为了实际项目的效率考虑,需要考虑在函数中分配一个内存区域.大概写法如下 //M ...

  4. C# HttpWebRequest 绝技 根据URL地址获取网页信息

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  5. c#利用HttpWebRequest获取网页源代码

    c#利用HttpWebRequest获取网页源代码,搞了好几天终于解决了,直接获取网站编码进行数据读取,再也不用担心乱码了! 命名空间:Using System.Net private static ...

  6. 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录

    利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...

  7. 黄聪:C#获取网页HTML内容的三种方式

    C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse. 方法一:使用WebClient static void ...

  8. C# 获取网页信息

    获取网页源码 ///通过HttpWebResponse public string GetUrlHtml(string url) { string strHtml = string.Empty; Ht ...

  9. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

随机推荐

  1. 【转载并整理】mysql分页方法

    http://blog.csdn.net/bestcleaner/article/details/52993468

  2. Spring boot validation校验

    使用 Hibernate validator 的步骤:1. 在 Pojo 类的字段上, 加上 Hibernate validator 注解2. 在Controller 函数的形参前加上 @Valid ...

  3. MySQL数据库优化详解(收藏)

    MySQL数据库优化详解 mysql表复制 复制表结构+复制表数据mysql> create table t3 like t1;mysql> insert into t3 select * ...

  4. Atitit atiplat_reader 基于url阅读器的新特性

    Atitit atiplat_reader 基于url阅读器的新特性 1.1. feature功能特性1 1.2. note1 1.1. feature功能特性 支持url数据源,实际就是只支持一层连 ...

  5. github pull request

    https://stackoverflow.com/questions/14680711/how-to-do-a-github-pull-request https://help.github.com ...

  6. numpy数组-标准化数据

    标准化数据的公式: (数据值 - 平均数) / 标准差 import numpy as np employment = np.array([ 55.70000076, 51.40000153, 50. ...

  7. package.json中配置浏览器

    "browserlist":[ ">=1%", "last 2 versions" ],

  8. java中的动态加载和热替换

    https://blog.csdn.net/u010833547/article/details/54312052 ****************************************** ...

  9. 8个超炫酷仿苹果应用的HTML5动画

    苹果的产品一直以精美的UI著称,无论是软件应用还是硬件设备.本文主要分享了8个很不错的HTML5动画应用,这些动画正式模仿了苹果的各类应用,有焦点图.钟表.菜单等HTML5应用和jQuery插件,大家 ...

  10. Python爬网——获取安卓手机统计数据

    [本文出自天外归云的博客园] 1. 在安卓网上对热门机型进行爬网,取前五十: # -*- coding: utf-8 -*- import requests,re from bs4 import Be ...