Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:47:33+00:00 2026-06-06T16:47:33+00:00

I have searched various forums and links but havn’t got anything helpful yet. Actually

  • 0

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

    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.Credentials = new NetworkCredential("usrname", "password", "domain");
        client.DownloadFile("https://some.server/file.zip", filepath);
    }
    catch (Exception exp)
    {
        Response.Write(exp.ToString());
    }
}

Server Error in ‘/WebSite1’ Application.

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.

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

Source Error:

Line 80: WebClient client = new WebClient();

Line 81: client.Credentials = new NetworkCredential(“username”, “password”, “domain”);

Line 82: client.DownloadFile(“https://some.server/file.zip”, filepath);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:47:36+00:00Added an answer on June 6, 2026 at 4:47 pm

    Use this class derived from WebClient. It will always pass the cookies with every request.

    class WebClientWithCookies: WebClient
    {
        private CookieContainer _container = new CookieContainer();
    
        protected override WebRequest GetWebRequest(Uri address) 
        {
            HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest; 
    
            if (request != null) 
            { 
                request.Method = "Post";
                request.CookieContainer = _container; 
            } 
    
            return request;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched the documentation and tried various techniques but have't found a suitable
I have searched for and found various q/a on this error, but have not
I have searched the Internet and stackoverflow.com, but got nothing. Who can give me
I have searched through various questions related to this on this website but i
I have searched high and low and tried various patterns but it seems I
I searched and searched google and various forums for the problem and have seen
I've searched various posts and forums but can't find the right answer. I need
I have searched for a general solution to this but only find answers to
I have searched and searched for this but I think my terminology isn't correct
I have searched apple's documentation and other posts on Stack Overflow, but I'm still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.