Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have searched various forums and links but havn't got anything helpful yet.

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.

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

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

I m able to download images from the server but those are not protected.

the code i have developed so far is

C#
    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&password=pass&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.DownloadFile("https://some.server/file.zip", filepath);
    }
    catch (Exception exp)
    {
        Response.Write(exp.ToString());
    }
}


Can someone help.
[Removed Urgency]
Thanks in advance
Posted
Updated 24-Jun-12 22:56pm
v3

1 solution

Solved
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900