Click here to Skip to main content
15,039,149 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 12 May 2011

Tagged as

Stats

49.4K views
8 bookmarked

WebClient Class with Cookies

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 May 2011CPOL2 min read
The WebClient class can be extended to support cookies by overriding the GetWebRequest method and implementing a CookieContainer.

The WebClient class can be used to automate the login to a web application. If the web application requires the client to enable cookies, which is often the case when the application uses session state, the application returns an error to the client indicating that cookies must be enabled. The WebClient class can be extended to support cookies by overriding the GetWebRequest method and implementing a CookieContainer. This allows the client to login to the application and maintain the cookie state for subsequent calls to the web application.

To demonstrate this, we should load Visual Studio (2008/2010) and create a new console application. Add a new class and name it WebClientWithCookies. The class should look as follows:

C#
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;
    }
}

The override in the class provides access to the HttpWebRequest reference which facilitates the access to the CookieContainer property. A new CookieContainer object is created and associated to the Request object. This allows the client to store the cookies from the web application in the CookieContainer property of the request. We also set the request method to use POST (this is not needed if the web application allows GET requests).

Now, open the Program class of the console application and update the code to look as follows:

C#
class Program
{
    private static string _cookies=string.Empty;
    static void Main(string[] args)
    {
        string url = "replace with url here";
        Post(url);
        Console.WriteLine("Hit return to exit");
        Console.ReadLine();
    }

    static void client_OpenReadCompleted(object sender, 
                System.Net.OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            using (Stream stream = e.Result)
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
            
            WebClientWithCookies client = sender as WebClientWithCookies;
            
            if (client != null)
            {
                _cookies = client.ResponseHeaders["Set-Cookie"];
                Console.WriteLine(_cookies);
            }
        }
        else
        {
            Console.WriteLine(e.Error.Message);
        }
    }
       

    private static void Post(string url)
    {
        using (WebClientWithCookies client = new WebClientWithCookies())
        {
          client.OpenReadCompleted += 
             new OpenReadCompletedEventHandler(client_OpenReadCompleted);
          var uri = new Uri(url, UriKind.RelativeOrAbsolute);
          client.Headers.Add("Cookie",_cookies);
          client.OpenReadAsync(uri);
        }
    }
}

The Post method instantiates a web client (with cookies support) object to post the request to the web application. You should also notice that we add a request header to include any cookies that had already been persisted. The call to the web application is made asynchronous, and the handler client_OpenReadCompleted handles the result of the request. If there are no errors, the handler reads the stream and displays the response on the console. The client reference is then used to read the response headers. Here we are using Set-Cookie header name which is used by ASP.NET applications. You may need to inspect the response headers using a tool like Fiddler. The inspection can help you identify the key name for the cookie header from the system that you are using. The cookie in the response is saved in a variable. This is necessary because on any other sub-sequent call to the web application, we need to send the cookie back to the server. If the cookie is not sent back to the server, the web application will redirect back to the login page.

I hope this helps.

License

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

Share

About the Author

ozkary
Architect OG-BITechnologies
United States United States
Software engineer, author & speaker who enjoys mentoring, learning, speaking and sharing with others about software development technologies. Microsoft MVP.

My Blog

Comments and Discussions

 
QuestionPost Method Error Pin
yangzn12-May-11 16:18
Memberyangzn12-May-11 16:18 
AnswerRe: Post Method Error Pin
ozkary13-May-11 4:01
Memberozkary13-May-11 4:01 
For a POST, the parameters can be passed the same way you pass a query string. When you get a length required, that means that you need to add a content-length header. You may want to add the following:


wc is the WebClient instance:

//headers
wc.Headers.Add(Net.HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
wc.Headers.Add(Net.HttpRequestHeader.ContentLength,LENGTH_OF_THE_QUERY_STRING)

//other headers that you may need
wc.Headers.Add(Net.HttpRequestHeader.Accept, "*/*")
wc.Headers.Add(Net.HttpRequestHeader.KeepAlive, "1")

hope that helps

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.