分类: C/C++
2008-08-01 17:09:26
HttpWebRequest hwr=(HttpWebRequest)WebRequest.Create();
创建了这个对象后,就可以通过HttpWebRequest属性,设置很多HTTP标头字段的内容,如hwr.AddRange(100,1000);设置接收对象的范围为100-1000字节。
HttpWebReques对象使用GetResponse()方法时,会返回一个HttpWebResponse对象,为提出HTTP返回报文信息,需要使用HttpWebResponse的GetResponseStream()方法,该方法返回一个Stream对象,可以读取HTTP返回的报文,如:首先定义一个Strean 对象
public System.IO.Stream ns; 然后
ns=hwr.GetResponse ().GetResponseStream ();即可创建Stream对象。
有了以上的准备知识后下面开始设计我们的多线程互联网文件的下载程序,首先打开Visual Studio.Net集成开发环境,选择“文件”、“新建”、“项目”,然后选择“Visual C#项目”,在向导右边列表框中选中“Windows 应用程序”,输入项目名称,如本例为:httpftp,然后选择“确定”按钮,向导自动生成了一个Windows应用程序项目。首先打开窗口设计器设计应用程序窗口,增加如下控件:
一个列表框 listBox1
三个文本标签 label1-label3
三个文本框 textBox1-textBox3
一个开始接收按钮 button1
设计好的窗口如下图:

控件定义代码是:
public System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
打开Form1的代码编辑器,增加如下的命名空间:
using System.Net;//网络功能
using System.IO;//流支持
using System.Threading ;//线程支持
增加如下的程序变量:
public bool[] threadw; //每个线程结束标志
public string[] filenamew;//每个线程接收文件的文件名
public int[] filestartw;//每个线程接收文件的起始位置
public int[] filesizew;//每个线程接收文件的大小
public string strurl;//接受文件的URL
public bool hb;//文件合并标志
public int thread;//进程数
定义一个HttpFile类,用于管理接收线程,其代码如下:
public class HttpFile
{
public Form1 formm;
public int threadh;//线程代号
public string filename;//文件名
public string strUrl;//接收文件的URL
public FileStream fs;
public HttpWebRequest request;
public System.IO.Stream ns;
public byte[] nbytes;//接收缓冲区
public int nreadsize;//接收字节数
public HttpFile(Form1 form,int thread)//构造方法
{
formm=form;
threadh=thread;
}
~HttpFile()//析构方法
{
formm.Dispose ();
}
public void receive()//接收线程
{
filename=formm.filenamew[threadh];
strUrl=formm.strurl;
ns=null;
nbytes= new byte[512];
nreadsize=0;
formm.listBox1 .Items .Add ("线程" threadh.ToString () "开始接收");
fs=new FileStream (filename,System.IO.FileMode.Create);
try
{
request=(HttpWebRequest)HttpWebRequest.Create (strUrl);
//接收的起始位置及接收的长度
request.AddRange(formm.filestartw [threadh],
formm.filestartw [threadh] formm.filesizew [threadh]);
ns=request.GetResponse ().GetResponseStream ();//获得接收流
nreadsize=ns.Read (nbytes,0,512);
while (nreadsize>0)
{
fs.Write (nbytes,0,nreadsize);
nreadsize=ns.Read (nbytes,0,512);
formm.listBox1 .Items .Add ("线程" threadh.ToString () "正在接收");
}
fs.Close();
ns.Close ();
}
catch (Exception er)
{
MessageBox.Show (er.Message );
fs.Close();
}
formm.listBox1 .Items.Add ("进程" threadh.ToString () "接收完毕!");
formm.threadw[threadh]=true;
}
}
该类和Form1类处于统一命名空间,但不包含在Form1类中。下面定义“开始接收”按钮控件的事件响应函数:
private void button1_Click(object sender, System.EventArgs e)
{
DateTime dt=DateTime.Now;//开始接收时间
textBox1.Text =dt.ToString ();
strurl=textBox2.Text .Trim ().ToString ();
HttpWebRequest request;
long filesize=0;
try
{
request=(HttpWebRequest)HttpWebRequest.Create (strurl);
filesize=request.GetResponse ().ContentLength;//取得目标文件的长度
request.Abort ();
}
catch (Exception er)
{
MessageBox.Show (er.Message );
}
// 接收线程数
thread=Convert.ToInt32 (textBox4.Text .Trim().ToString (),10);
//根据线程数初始化数组
threadw=new bool [thread];
filenamew=new string [thread];
filestartw=new int [thread];
filesizew=new int[thread];
//计算每个线程应该接收文件的大小
int filethread=(int)filesize/thread;//平均分配
int filethreade=filethread (int)filesize%thread;//剩余部分由最后一个线程完成
//为数组赋值
for (int i=0;i
合并文件的线程hbfile定义在Form1类中,定义如下:
public void hbfile()
{
while (true)//等待
{
hb=true;
for (int i=0;i0)
{
fs.Write (bytes,0,readfile);
}
else
{
break;
}
}
fstemp.Close ();
}
fs.Close ();
DateTime dt=DateTime.Now;
textBox1.Text =dt.ToString ();//结束时间
MessageBox.Show ("接收完毕!!!");
}
至此,一个多线程下载文件的程序就大功告成了,注意在输入本地文件名时,应按如下格式输入:“c:\\test\\httpftp\\bin\\d.htm”,
因”\”后的字符在C#中是转义字符,线程数并非越大越好,一般5个线程就可以了,该程序在Visual Studio.Net 2002开发环境及Windows xp 操作系统上通过。
下载本文示例代码