4/25/11

WebClient Class with Cookies

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 sub-sequent 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:
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 it 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:
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 we may 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 calls 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.

og-bit.com

4/20/11

Start Multiple Projects with Visual Studio 2008 and 2010

By default, Visual Studio allows us to set a single Startup project on a solution. There are instances when we need to start multiple projects. A typical case is when we have a client and server console application, and we want to debug/start both at the same time.
There is a feature on Visual Studio (2008 and 2010) that allows us to start multiple projects within a solution. To configure multiple Startup projects follow these steps:
·         Right Click on the solutions name
·         Select properties
·         On the Left panel, click on Common Properties and select Startup Project
·         On the right panel, check Multiple startup projects
·         For each project you want to start, set the action to Start or Start without debugging (do no step into the code). For the projects you do not want to start, set the action to None
·         If you need to start the projects in a particular order, you should stock up the projects in the order in which they should start. For example, you may need the server project to start first, and the client project should start second. To move the order of the projects, select the project name (under the Project column) and click the Up/Down arrow. The projects on the top of the list start first.
·         Click the OK button
You can now notice that none of the project  names is in boldfaced letters, but if you look at the solution name, you can see that the solution name is on boldfaced letter. This is a quick way to identify when multiple projects are set as startup projects.  To select only a single startup project, just click on the project name and click set as Startup Project.  This unselects the multiple startup project options from the solutions properties, and it sets the single startup project selection.
I hope this tip helps

og-bit.com