none
ˊ請問如何透過windows Form, 下載一個URL的檔案,並將檔案儲存在client端 RRS feed

解答

  • 利用WebClient.DownloadData 方法
    以下為MSDN上的範例

    Console.Write("\nPlease enter a URI (for example, http://www.contoso.com): ");
    string remoteUri = Console.ReadLine();

    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Download home page data.
    Console.WriteLine("Downloading " + remoteUri);                       
    // Download the Web resource and save it into a data buffer.
    byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);

    // Display the downloaded data.
    string download = Encoding.ASCII.GetString(myDataBuffer);
    Console.WriteLine(download);
                       
    Console.WriteLine("Download successful.");

    2006年3月7日 上午 04:03
  • 這樣你就得用HttpWebRequest和HttpWebResponse這兩個類別了

    // Creates an HttpWebRequest with the specified URL.
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        // Sends the HttpWebRequest and waits for the response.           
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        // Gets the stream associated with the response.
        Stream receiveStream = myHttpWebResponse.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        // Pipes the stream to a higher level stream reader with the required encoding format.
        StreamReader readStream = new StreamReader( receiveStream, encode );
    Console.WriteLine("\r\nResponse stream received.");
        Char[] read = new Char[256];
        // Reads 256 characters at a time.   
        int count = readStream.Read( read, 0, 256 );
        Console.WriteLine("HTML...\r\n");
        while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                String str = new String(read, 0, count);
                Console.Write(str);
                count = readStream.Read(read, 0, 256);
            }
        Console.WriteLine("");
        // Releases the resources of the response.
        myHttpWebResponse.Close();
        // Releases the resources of the Stream.
        readStream.Close();

    2006年3月7日 上午 06:22

所有回覆

  • 利用WebClient.DownloadData 方法
    以下為MSDN上的範例

    Console.Write("\nPlease enter a URI (for example, http://www.contoso.com): ");
    string remoteUri = Console.ReadLine();

    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Download home page data.
    Console.WriteLine("Downloading " + remoteUri);                       
    // Download the Web resource and save it into a data buffer.
    byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);

    // Display the downloaded data.
    string download = Encoding.ASCII.GetString(myDataBuffer);
    Console.WriteLine(download);
                       
    Console.WriteLine("Download successful.");

    2006年3月7日 上午 04:03
  • 感謝您這麼快就提供資訊

    利用WebClient.DownloadData 方法, 可在windows form中執行.

     

     

    另外, 假設是在.net compact framework的環境下, 似乎不支援WebClient類別,

    目前是否有其他的類別可代替這個功能?

    謝謝

     

    2006年3月7日 上午 05:38
  • 這樣你就得用HttpWebRequest和HttpWebResponse這兩個類別了

    // Creates an HttpWebRequest with the specified URL.
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        // Sends the HttpWebRequest and waits for the response.           
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        // Gets the stream associated with the response.
        Stream receiveStream = myHttpWebResponse.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        // Pipes the stream to a higher level stream reader with the required encoding format.
        StreamReader readStream = new StreamReader( receiveStream, encode );
    Console.WriteLine("\r\nResponse stream received.");
        Char[] read = new Char[256];
        // Reads 256 characters at a time.   
        int count = readStream.Read( read, 0, 256 );
        Console.WriteLine("HTML...\r\n");
        while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                String str = new String(read, 0, count);
                Console.Write(str);
                count = readStream.Read(read, 0, 256);
            }
        Console.WriteLine("");
        // Releases the resources of the response.
        myHttpWebResponse.Close();
        // Releases the resources of the Stream.
        readStream.Close();

    2006年3月7日 上午 06:22