23

I would like the VB.net WebClient to remember cookies.

I have searched and tried numerous overloads classes.

I want to login to a website via POST, then POST to another page and get its contents whilst still retaining my session.

Is this possible with VB.net without using WebBrowser control ?

I tried Chilkat.HTTP and it works, but I want to use .Net libraries.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jeremy
  • 3,880
  • 3
  • 35
  • 42
  • 2
    Possible duplicate of [Accept Cookies in WebClient?](https://stackoverflow.com/questions/14551345/accept-cookies-in-webclient) – Migol Apr 01 '18 at 11:05

2 Answers2

52

Create a new class the inherits from WebClient that stores the CookieContainer like @Guffa says. Here's code that I use that does that and also keeps the referer alive:

Public Class CookieAwareWebClient
    Inherits WebClient

    Private cc As New CookieContainer()
    Private lastPage As String

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
        Dim R = MyBase.GetWebRequest(address)
        If TypeOf R Is HttpWebRequest Then
            With DirectCast(R, HttpWebRequest)
                .CookieContainer = cc
                If Not lastPage Is Nothing Then
                    .Referer = lastPage
                End If
            End With
        End If
        lastPage = address.ToString()
        Return R
    End Function
End Class

Here's the C# version of the above code:

using System.Net;
class CookieAwareWebClient : WebClient
{
    private CookieContainer cc = new CookieContainer();
    private string lastPage;

    protected override WebRequest GetWebRequest(System.Uri address)
    {
        WebRequest R = base.GetWebRequest(address);
        if (R is HttpWebRequest)
        {
            HttpWebRequest WR = (HttpWebRequest)R;
            WR.CookieContainer = cc;
            if (lastPage != null)
            {
                WR.Referer = lastPage;
            }
        }
        lastPage = address.ToString();
        return R;
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Will try this. Thankyou. Its similar to the other inherit, then overload classes ive found. – Jeremy May 14 '10 at 10:05
  • I've been using this for years with absolute success. If its not working for you then its not a cookie/session issue but probably the website that you're spidering is using javascript to modify the form-state. If that's the case either check over the javascript or just use a packet monitor or fiddler to see what's actually being sent over the wire. – Chris Haas May 14 '10 at 12:48
  • can ne1 post this in c#, and my problm is also similar, i posted a question for this but i didn't get ne relevant answers..still search for the answer.. – FosterZ Dec 16 '10 at 14:18
  • When I try to use this class with a silverlight 4 project I get a TypeLoadException. Inheritance security rules violated while overriding member. Any ideas? – Julio Garcia Oct 17 '11 at 16:29
  • See this post http://stackoverflow.com/questions/7369689/silverlight-4-subclassing-webclient – Chris Haas Oct 17 '11 at 16:57
  • 1
    It's a wonder why microsoft do not release webclient with cookie awareness from the beginning. – user4951 Jan 29 '12 at 09:14
  • @Jim Thio, while I wish it did support it natively, I think `WebClient` was intended for very simple situations. While you could argue that cookies seem an obvious need in all situations, I think other people could argue the same for other features not supported. If MS included all of these it would turn right back into the `HttpWebRequest` object that they were trying to simplify in the first place. So instead they made what they considered the most basic features and allowed us to extend it when and where needed. – Chris Haas Jan 29 '12 at 15:00
  • The HttpWebRequest object doesn't natively support cookies either and you have to manually manage cookies, still a pain like with WebClient here. If you prefer not to have to manage cookies yourself, one option is a third party library, described here (along with info on HttpRequest & HttpResponse & cookie classes): http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm, and you can get the latest version of that library here (use WestWind.InternetTools.HttpClient class that's part of the utilities DLL assembly library): http://www.west-wind.com/WestwindWebToolkit/ – David May 02 '12 at 07:07
  • 2
    Just wanted to point out that if you wanted to use the WebClient class with cookie support but wanted to pass in your own cookie (say an existing session from an HttpWebRequest/Response session), you can further extend the example above by adding constructors. An example of what I did here: public CookieAwareWebClient() { cc = new CookieContainer(); } public CookieAwareWebClient(CookieCollection cookies) { cc = new CookieContainer(); cc.Add(cookies); } – David May 02 '12 at 07:10
5

You can't make the WebClient class remember the cookies, you have to get the cookie container from the response and use it in the next request.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005