一個下載網頁的程式,很不完善,希望各位可以幫我完善一下!
列如:下載時設定編碼;
非同步下載;
顯示下載進度等等。
我在這裡先謝謝各位了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebRequest hwr = HttpWebRequest.Create("http://www.sina.com.cn");
WebResponse wp = hwr.GetResponse();
using (System.IO.FileStream fs = new System.IO.FileStream(AppDomain.CurrentDomain.BaseDirectory + "abc.html", System.IO.FileMode.Create))
{
System.IO.Stream str = wp.GetResponseStream();
byte[] b = new byte[1024];
int len = 0;
while (true) {
len = str.Read(b, 0, 1024);
if (len == 0) break;
fs.Write(b, 0, len);
}
fs.Flush();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebRequest hwr = HttpWebRequest.Create("http://www.sina.com.cn");
WebResponse wp = hwr.GetResponse();
using (System.IO.FileStream fs = new System.IO.FileStream(AppDomain.CurrentDomain.BaseDirectory + "abc.html", System.IO.FileMode.Create))
{
System.IO.Stream str = wp.GetResponseStream();
byte[] b = new byte[1024];
int len = 0;
while (true) {
len = str.Read(b, 0, 1024);
if (len == 0) break;
fs.Write(b, 0, len);
}
fs.Flush();
}
}
}
}
WebClient client = new WebClient();
//下面這一句好像沒有用
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead("http://www.cnblogs.com");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();
//下面這一句好像沒有用
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead("http://www.cnblogs.com");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();
下面的程式碼是非同步下載網頁!並且可以設定編碼,我試過了,下載部落格園的首頁用(UTF-8)沒有問題,但是下新浪和網易首頁用(GB3212),記事本開啟沒有問題,但是用IE和FireFox等下載工具,開啟是亂碼!我設定在瀏覽器中設定編號,也不管用,不知道是為什麼??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Class1
{
public static void Main(){
System.Net.WebClient wc = new System.Net.WebClient();
wc.OpenReadAsync(new Uri("http://www.cnblogs.com"));
wc.OpenReadCompleted += new System.Net.OpenReadCompletedEventHandler(wc_OpenReadCompleted);
Console.WriteLine("請稍等。。");
Console.Read();
}
static void wc_OpenReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e){
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "aa.html",false)) {
System.IO.StreamReader sr = new System.IO.StreamReader(e.Result, Encoding.GetEncoding("utf-8"));
sw.Write(sr.ReadToEnd());
sw.Flush();
}
Console.WriteLine("你好,你的資料已經下載完成!");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Class1
{
public static void Main(){
System.Net.WebClient wc = new System.Net.WebClient();
wc.OpenReadAsync(new Uri("http://www.cnblogs.com"));
wc.OpenReadCompleted += new System.Net.OpenReadCompletedEventHandler(wc_OpenReadCompleted);
Console.WriteLine("請稍等。。");
Console.Read();
}
static void wc_OpenReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e){
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "aa.html",false)) {
System.IO.StreamReader sr = new System.IO.StreamReader(e.Result, Encoding.GetEncoding("utf-8"));
sw.Write(sr.ReadToEnd());
sw.Flush();
}
Console.WriteLine("你好,你的資料已經下載完成!");
}
}
}