一個下載網頁的程式

來源:互聯網
上載者:User

一個下載網頁的程式,很不完善,希望各位可以幫我完善一下!
列如:下載時設定編碼;
         非同步下載;
         顯示下載進度等等。
我在這裡先謝謝各位了!

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();
            }
        }
    }
}

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();

下面的代碼是非同步下載網頁!並且可以設定編碼,我試過了,下載部落格園的首頁用(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("你好,你的資料已經下載完成!");
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.