Pavel

Pavel

  • 1.6k
  • 170
  • 51.2k

Download .pdf from website with cookies

Mar 3 2015 5:37 AM

Hello,

I've met problem while trying to download .pdf file form site with cookies.

As this website contain cookies, using of WebClient doesn't work, so I employed CookieAwareWebClient, that I've found somewhere on web. Here is its content:

   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;

        }

    }

 Using DownloadFile method works fine with  CookieAwareWebClient, but when I try to use DownloadFileAsync, new problem arises: DownloadFileCompleted event isn't fired.

I think that Cookie transaction isn't properly processed in CookieAwareWebClient object and solution could be to get in some way form the web site the cookie collection and assign it to the CookieContainer property of CookieAwareWebClient object.

For the moment I didn't find how to do it.

Thank in advance.

Pavel.