Maintaining same session with separate web requests from C# code

Create separate web requests with the same server session

Recently I started a small project for image optimization which relies on public image optimization services available online. For now it supports Yahoo SmushIt and Compressor.io services, but it will be extended over the time.

Project is open-source and hosted at GitHub https://github.com/dejanstojanovic/ImageOptimization.

Both these two services for which I wrote implementation are working basically by sending images and in response they return JSON with result paths and some additional data regrading the optimization process.

From the response of the image post request you get the path of the result, optimized image, but when you try to download it with a new request, you will get 404.

This issue occurs because upload and download request are not using the same session as the session ID cookie is not shared between these two requests. Most logical thing is to share somehow cookie from first request to all other requests.

My fist approach is to pickup all the headers from upload request and add those key-values to download request. The thing is that there is much easier way to share the session among multiple web requests with a Cookie container.

Funny thing is that request does not initialize Cookie container by itself. You have to instantiate new CookieContainer it and assign it to a web request. After request returns it's response, you can assign the same CookieContainer to a new request you are about to make and the second request will be actually in the same server session as the previous one.

If you check class Optimizer at https://github.com/dejanstojanovic/ImageOptimization/blob/master/ImageOptimization/Base/Optimizer.cs and check method CreateRequest you will see that new CookieContainer is instantiated and assigned to a web request which is about to send/upload file to a optimization service end point

 protected HttpWebRequest CreateRequest(string inputFile)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(this.ServiceUrl);
            CookieContainer cookieContainer = new CookieContainer();
            webRequest.CookieContainer = cookieContainer;
            .
            .
            .
            return webRequest;
        }
    

As soon as this request returns a JSON result with all the details of optimization and download path, another method is invoked to download the result, but as I mentioned before it needs to make a request withing the same session. This is achieved by assigning the same instance of CookieContainer used in the first request

        protected void DownloadResult(CookieContainer cookieContainer, string sourceUrl, string inputFilePath, string responseFilename = null)
        {
            HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create(sourceUrl);
            downloadRequest.CookieContainer = cookieContainer;
            .
            .
            .
        }
    

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article