











看到上次一个圆子里的弟兄提到的慢速VOA,感觉还不错,但是不想一个一个下载,所以写了段代码。
好长时间都不写代码了,手生了!
现在写代码都是为了自己用!让高手见笑了!
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Net;
9
using System.IO;
10
using System.Text.RegularExpressions;
11
12
namespace VOA
13
{
14
public delegate void DelegateProgressBarPerformStep(long i);
15
public partial class Form1 : Form
16
{
17
public DelegateProgressBarPerformStep DelegateProgressBarPerformStep;
18
long iConLength;
19
public Form1()
20
{
21
InitializeComponent();
22
}
23
24
public void ProgressBarPerformStep(long i)
25
{
26
this.Text = "Processing
" + string.Format("Finished: {0} / {1}", i , iConLength);
27
}
28
29
private void btnGetList_Click(object sender, EventArgs e)
30
{
31
GetList(textBox1.Text);
32
}
33
34
private void button1_Click(object sender, EventArgs e)
35
{
36
DownLoad(listView1.SelectedItems[0].SubItems[1].Text, listView1.SelectedItems[0].Text);
37
}
38
39
private void GetList(string strUrl)
40
{
41
string strHtml = String.Empty;
42
HttpWebRequest rt = null;
43
HttpWebResponse rs = null;
44
Stream stream = null;
45
StreamReader sr = null;
46
string temp = string.Empty;
47
48
49
try
50
{
51
rt = (HttpWebRequest)WebRequest.Create(strUrl);
52
rs = (HttpWebResponse)rt.GetResponse();
53
stream = rs.GetResponseStream();
54
sr = new StreamReader(stream, Encoding.UTF8);
55
strHtml = sr.ReadToEnd();
56
}
57
catch (Exception ee)
58
{
59
MessageBox.Show("发生异常" + ee.Message);
60
}
61
finally
62
{
63
sr.Close();
64
stream.Close();
65
rs.Close();
66
}
67
68
string regexStr = "";
69
int counter = 0;
70
int i = 0;
71
string strKey = "";
72
string strText = "";
73
try
74
{
75
regexStr = @"\<a href=.*\w{4}\-\w{2}\-\w{2}";
76
temp = "";
77
counter = 0;
78
MatchCollection mc = Regex.Matches(strHtml, regexStr);
79
counter = mc.Count;
80
for (i = 0; i < counter; ++i)
81
{
82
temp = mc[i].ToString();
83
strKey = temp.Substring(9, 13);
84
strKey = "http://bulo.hjenglish.com" + strKey;
85
int itemp = temp.IndexOf("【慢速VOA】");
86
87
strText = temp.Substring(itemp);
88
listView1.Items.Add(strText);
89
listView1.Items[i].SubItems.Add(strKey);
90
}
91
mc = null;
92
}
93
catch (Exception ee)
94
{
95
MessageBox.Show(ee.Message);
96
}
97
}
98
99
private void DownLoad(string strUrl, string strFile)
100
{
101
string strHtml = String.Empty;
102
HttpWebRequest rt = null;
103
HttpWebResponse rs = null;
104
Stream stream = null;
105
StreamReader sr = null;
106
string temp = string.Empty;
107
108
109
try
110
{
111
rt = (HttpWebRequest)WebRequest.Create(strUrl);
112
rs = (HttpWebResponse)rt.GetResponse();
113
stream = rs.GetResponseStream();
114
sr = new StreamReader(stream, Encoding.UTF8);
115
strHtml = sr.ReadToEnd();
116
}
117
catch (Exception ee)
118
{
119
MessageBox.Show("发生异常" + ee.Message);
120
}
121
finally
122
{
123
sr.Close();
124
stream.Close();
125
rs.Close();
126
}
127
128
string regexStr = "";
129
int counter = 0;
130
int i = 0;
131
132
GetMP3
151
152
try
153
{
154
regexStr = @"参考答案:.*";
155
temp = "";
156
counter = 0;
157
MatchCollection mc = Regex.Matches(strHtml, regexStr);
158
counter = mc.Count;
159
for (i = 0; i < counter; ++i)
160
{
161
writeTxt(mc[i].ToString(),strFile);
162
}
163
mc = null;
164
}
165
catch (Exception ee)
166
{
167
MessageBox.Show(ee.Message);
168
}
169
170
MessageBox.Show("下载完成");
171
}
172
173
private void writeTxt(string strBody,string strFile)
174
{
175
StreamWriter sw;
176
try
177
{
178
sw = File.CreateText(strFile + ".txt");//txt文件会创建到跟目录下的BIN→Debug下
179
}
180
catch
181
{
182
Console.WriteLine("不能创建文件!");
183
return;
184
}
185
186
sw.Write("{0} ",strBody);
187
sw.Close();
188
189
}
190
191
private void downloadMP3(string strUrl,string strFile)
192
{
193
//打开上次下载的文件或新建文件
194
long lStartPos =0;
195
System.IO.FileStream fs;
196
if (System.IO.File.Exists(strFile))
197
{
198
fs= System.IO.File.OpenWrite(strFile);
199
lStartPos=fs.Length;
200
fs.Seek(lStartPos,System.IO.SeekOrigin.Current); //移动文件流中的当前指针
201
}
202
else
203
{
204
fs = new System.IO.FileStream(strFile, System.IO.FileMode.Create);
205
lStartPos =0;
206
}
207
208
long iFinished = lStartPos;
209
210
//打开网络连接
211
try
212
{
213
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl);
214
215
if ( lStartPos>0)
216
request.AddRange((int)lStartPos); //设置Range值
217
218
//向服务器请求,获得服务器回应数据流
219
System.IO.Stream ns= request.GetResponse().GetResponseStream();
220
iConLength = request.GetResponse().ContentLength;
221
byte[] nbytes = new byte[512];
222
int nReadSize=0;
223
nReadSize=ns.Read(nbytes,0,512);
224
225
while( nReadSize >0)
226
{
227
iFinished += nReadSize;
228
fs.Write(nbytes,0,nReadSize);
229
nReadSize=ns.Read(nbytes,0,512);
230
//progressBar1.Invoke(this.DelegateProgressBarPerformStep, new object[] { iFinished });
231
}
232
fs.Close();
233
ns.Close();
234
235
}
236
catch(Exception ex)
237
{
238
fs.Close();
239
MessageBox.Show("下载过程中出现错误:"+ex.ToString());
240
}
241
}
242
}
243
}
244
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。