31

I want to download something using a WebClient object in C#, but the download domain requires me to be logged in. How can I log in and keep session data using WebClient? I know how to post data with WebClient.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

2 Answers2

50

If the problem you are having is you can authenticate but you cant keep the authentication cookie here is a cookie aware version of WebClient.

private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
        : this(new CookieContainer())
    { }
    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);

        var castRequest = request as HttpWebRequest;
        if (castRequest != null)
        {
            castRequest.CookieContainer = this.CookieContainer;
        }

        return request;
    }
}

EDIT: The link you gave me uses forms authentication with HTTP POST, I don't have the time to walk though it but at least it gives you a start with Google.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 1
    i found this. but what do i need to do ? do i have to authenticate via httpwebrequest or what ? – Furkan Gözükara Jan 19 '11 at 21:17
  • It depends on what authentication system the site you are connecting to uses. See some of the other responses on tips for authenticating – Scott Chamberlain Jan 19 '11 at 21:22
  • 1
    I wish this post would include an example on the actual authentication process itself. – Shimmy Weitzhandler Jun 12 '13 at 13:41
  • 1
    @MonsterMMORPG the answer in http://stackoverflow.com/questions/11118712/webclient-accessing-page-with-credentials has similar solution, and there is a sample usage code. – hardywang Mar 20 '14 at 17:28
  • it would have saved me a lot of time if you would have put which namespaces to include in your code example. that's pretty important information to know... – Anthony Sep 05 '14 at 21:59
  • @Anthony If you type in to google `MSDN WebClient` the first result is [the MSDN page for WebClient](http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx) which includes the namespace. For any .NET class just type in `MSDN YourClassName` and the class you are looking for will be within the first 3 links. – Scott Chamberlain Sep 06 '14 at 00:26
-1

Take a look at using the Credentials property. E.g. if Basic authentication is used, you will have to set the property to an instance of NetworkCredential with the proper username and password.

The sample pointed to shows how to use the currently logged on user credentials for the request.

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • Okey, I see. The server uses Forms authentication. That requires you posting the username and password to the action url specified in the login page html. You must also use a cookie container in order to receive the auth cookie that comes with the response to that post. – Peter Lillevold Jan 19 '11 at 22:47
  • @MonsterMMORPG `DoesNotWorkNullReferenceException` – C4d Jul 14 '16 at 17:30