Thursday, August 04, 2011

How to use HttpWebRequest in .NET

Posted by Rahul Kharde at 10:41 PM
Internet applications can be classified broadly into two kinds: client applications that request information, and server applications that respond to information requests from clients. The classic Internet client-server application is the World Wide Web, where people use browsers to access documents and other data stored on Web servers worldwide. 

The System.Net.WebClient class provides functionality to upload data to or download data from the Internet or intranet or a local file system. The WebClient class provides many ways to download and upload data. The following table describes WebClient class methods and properties briefly.
Code :
        public class ClsDownloadFile
            // The stream of data retrieved from the web server
            private Stream strResponse;
            // The stream of data that we write to the harddrive
            private Stream strLocal;
            // The request to the web server for file information
            private HttpWebRequest webRequest;
            // The response from the web server containing information about the file
            private HttpWebResponse webResponse;

            public void DownloadFile(object startPoint, string DestinationPath, String SourcePath)
                    // Put the object argument into an int variable
                    int startPointInt = Convert.ToInt32(startPoint);
                    // Create a request to the file we are downloading
                    webRequest = (HttpWebRequest)WebRequest.Create(SourcePath);
                    // Set the starting point of the request
                    // Set default authentication for retrieving the file
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                    // Retrieve the response from the server
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    // Ask the server for the file size and store it
                    Int64 fileSize = webResponse.ContentLength;
                    // Open the URL for download
                    strResponse = webResponse.GetResponseStream();
                    // Create a new file stream where we will be saving the data (local drive)
                    if (startPointInt == 0)
                        strLocal = new FileStream(DestinationPath, FileMode.Create, FileAccess.Write, FileShare.None);
                        strLocal = new FileStream(DestinationPath, FileMode.Append, FileAccess.Write, FileShare.None);
                    // It will store the current number of bytes we retrieved from the server
                    int bytesSize = 0;
                    // A buffer for storing and writing the data retrieved from the server
                    byte[] downBuffer = new byte[2048];
                    // Loop through the buffer until the buffer is empty
                    while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                        // Write the data from the buffer to the local hard drive
                        strLocal.Write(downBuffer, 0, bytesSize);
                    // When the above code has ended, close the streams

Call Function Using blow code

ClsDownloadFile _ObjDownload = new ClsDownloadFile();

_ObjDownload.DownloadFile("0", @"D:\", @"E:\");

If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!

Kindly Bookmark and Share it:



Have any question? Feel Free To Post Below:


Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top