asp.net Download files from protected server
<p>I have searched various forums and links but havn't got anything helpful yet.</p>

<p>Actually i working towards developing a web application in ASP.net using C# that enables use to login on a remote server and then download files from that server.</p>

<p>Curently i m able to login to that server and see the content of the protected page.</p>

<p>But when it comes to download the file, i get an Unauthorize access error.</p>

<p>I m able to download images from the server but those are not protected.</p>

<p>the code i have developed so far is</p>

<pre><code>    protected void Unnamed1_Click(object sender, EventArgs e)
{
    try
    {

        string LOGIN_URL = "https://some.server/";

        // first, request the login form to get the value

        HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.KeepAlive = true;
        webRequest.UserAgent = userAgent;
        String received = Encoding.ASCII.GetString(Request.BinaryRead(Request.ContentLength));
        webRequest.ContentLength = received.Length;
        webRequest.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");
        StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(), Encoding.ASCII);
        string responseData = responseReader.ReadToEnd();
        responseReader.Close();
        string postData = "user=usr&amp;password=pass&amp;switch=Login";
        Response.Write(webRequest.Address);

        // have a cookie container ready to receive the forms auth cookie

        CookieContainer cookies = new CookieContainer();

        // now post to the login form

        webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;

        // write the form values into the request message

        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postData);
        requestWriter.Close();

        // we don't need the contents of the response, just the cookie it issues 

        webRequest.GetResponse().Close();



        // Download files

        WebProxy myProxy = new WebProxy("server.https.com", 443);
        //WebRequest request = WebRequest.Create("https://some.server/file.zip");
        WebRequest request = WebRequest.Create("https://some.server/file.zip");
        request.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");

        string filename = "file.zip";
        string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString();

        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential("usrname", "password", "domain");
        client.DownloadFile("https://some.server/file.zip", filepath);
    }
    catch (Exception exp)
    {
        Response.Write(exp.ToString());
    }
}
</code></pre>

<h2>Server Error in '/WebSite1' Application.</h2>

<p>The remote server returned an error: (401) Unauthorized. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>

<p>Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.</p>

<p>Source Error: </p>

<p>Line 80:             WebClient client = new WebClient();</p>

<p>Line 81:             client.Credentials = new NetworkCredential("username", "password", "domain");</p>

<p>Line 82:             client.DownloadFile("https://some.server/file.zip", filepath);</p>
