2
Vote

Add support for Credentials

description

Some sites require authentication, to implement this functionality I simply added this line of code.
 
    void DownloadFile(string url, string fileName)
    {
        WriteToScreen("Downloading File: " + url);
 
        WebRequest webRequest = WebRequest.Create(url);
        webRequest.Credentials = new NetworkCredential("SelectUser", "SelectedPassword"); //To-Add by Dreams
        WebResponse webResponse = null;
        Stream responseStream = null;
        try
        {
            webResponse = webRequest.GetResponse();
            responseStream = webResponse.GetResponseStream();
 
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                byte[] buffer = new byte[1024];
                int readSize;
                while ((readSize = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, readSize);
                }
            }
        }
        finally
        {
            if (responseStream != null)
                responseStream.Close();
 
            if (webResponse != null)
                webResponse.Close();
        }
    }
 
     DownloadURL string (string url)
     {
         WriteToScreen ("Downloading" + url);
         using (WebClient client = new WebClient ())
         {
             client.Credentials = new NetworkCredential ("SelectedUser", "SelectedPassword");  //To-Add by Dreams
             client.DownloadString string data = (url);
 
             return date;
         }
     }

comments

Merlin2001 wrote Sep 2, 2015 at 8:31 PM

I just ran into the same problem - credential support would be great!