我用HttpWebRequest对象,可以获取网页URL资源的代码,请问我可以只获取网页头几行代码就中断吗?因为全部获取太浪费时间了.

我希望只获取一点点代码就中断,释放对象资源。可是实现吗?
我之前写的函数,总是获取全部的代码,碰到大的网页,就很慢。
[69 byte] By [KAI3000-天天向上] at [2008-1-7]
# 1
你的客户程序没有任何问题,可以随时中断。

其实只有那个用于提供下载源的页面可能有问题。如果你用aspx读取资源用于下载,例如支持断点序传的下载程序,应该不断检查IsClientConnected属性,并且在客户端中断的时候立刻中断服务器端的下载程序。如果不这样写,例如那些简单地用Response.WriteFile 输出文件的程序,当输出的文件比较大时就把服务器给hang住了,即使客户端断开了,服务器程序仍然在那里死等。
# 2
请求时在http头中加入就可以了
Range: bytes=起始位置 - 终止位置
blackant2-乔峰 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 3
谢谢blackant2,我去试试看
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 4
看看我这样写对吗?

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://kai521.spaces.live.com/feed.rss");
req.AddRange(500);//只请求500字节。
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "GET";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream s = res.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(s, encode);

Response.Write(Server.HtmlEncode(sr.ReadToEnd()));
sr.Close();
res.Close();

------------------------------

结果似乎不对,还是全部读取出来了。慢~
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 5
sp1234(千万不要忘记普通人) :
你的客户程序没有任何问题,可以随时中断。

-------------------
谢谢,请问怎么随时中断呢?
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 6
/// <summary>
/// 文件下载
/// </summary>
/// <param name="url">连接</param>
/// <param name="fileName">本地保存文件名</param>
/// <param name="progressBar">进度条</param>
/// <param name="label">返回已经下载的百分比</param>
public void httpDownFile(string url,string fileName,System.Windows.Forms.ProgressBar progressBar,Label label)
{
stopDown=false;
Stream str=null,fs=null;
try
{
//获取下载文件长度
fileLength=getDownLength(url);
downLength=0;
if(fileLength>0)
{
WebClient DownFile=new WebClient();
str =DownFile.OpenRead(url);
//判断并建立文件
if(createFile(fileName))
{
byte[] mbyte = new byte[1024];
int readL=str.Read(mbyte,0,1024);
fs=new FileStream(fileName,FileMode.OpenOrCreate,FileAccess.Write);
//读取流
while(readL!=0)
// while(readL<1024)
{
if(stopDown)
break;
downLength+=readL;//已经下载大小
if (downLength>1024)//--------------------关键是这个,读取到N字节就停止了读取。
{
break;
}
fs.Write(mbyte,0,readL);//写文件
readL=str.Read(mbyte,0,1024);//读流
progressBar.Value=(int)(downLength*100/fileLength);
label.Text=progressBar.Value.ToString()+"%";
System.Windows.Forms.Application.DoEvents();
}
str.Close();
fs.Close();
}
}
}
catch(Exception ex)
{
if(str!=null)
str.Close();
if(fs!=null)
fs.Close();
// MessageBox.Show(ex.Message);
}
}
winner2050-winner at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 7
谢谢winner2050(winner),我看了你的代码,发现是使用了一个模拟参照的方法。
byte[] mbyte = new byte[1024];
int readL = str.Read(mbyte, 0, 1024);
根据readL模拟的进度,判断实际进度
这样模拟的,准确么?
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 8
我明白了。
byte[] mbyte = new byte[1024];
这是一个缓冲字符数组。

高明。我去try一把。
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...
# 9
我想知道WebClient的openRead是异步的还是同步的?
--------------------------
如是同步(不开新线程)的,那在读openRead方法完成之前(获取到网页全部的代码),Stream是不能打开的?这样的话,就不能满足我的 【只读取部分代码就中断】 的要求。
因为这个Steam还是读完了所有网页代码,碰到大网页仍然很慢。
KAI3000-天天向上 at 2007-10-1 > top of Msdn China Tech,.NET技术,ASP.NET...