alex_1995_henry
alex_1995_henry

Reputation: 49

How to set auth cookie for WebClient.downloadstring behind login form

I am trying to use WebClient.DownloadString() to scrape JSON data from a URL.

The issue is I find that programmatically accessing the URL: "secure.somesite.com.au/api/products/getprice?productName=Cornmeal" results in the site forcefully closing the connection. I believe this occurs because the auth cookie is not set.

How does one set a cookie? I've spent some time reading up on stackoverflow and codeproject, and no one is setting actual cookies, they're all setting username and passwords. I need to set the cookie so the site knows I should have access.

 using (var client = new CookieAwareWebClient())
        {
            Cookie cookie = new Cookie();
            cookie.Name = "SWI";
            cookie.Value = "kjuujj7kxPvEC-4fBt5yyzWOJnjhriuoOtZ6Z0Ww";
            cookie.Domain = ".secure.somesite.com.au";
            client.CookieContainer.Add(cookie);

            string r = client.DownloadString("https://secure.somesite.com.au/api/products/getprice?productName=Cornmeal");
        }

CookieAwareWebClient Class:

public class CookieAwareWebClient : WebClient
    {
        public CookieAwareWebClient()
        {
            CookieContainer = new CookieContainer();
        }
        public CookieContainer CookieContainer { get; private set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = (HttpWebRequest)base.GetWebRequest(address);
            request.CookieContainer = CookieContainer;
            return request;
        }
    }

PS. I have attempted to login with WebClient and all I get is the connection is closed forcefully. I think this is because if you are not logged already, requesting protected resources results in an error in WebClient and not just a string saying "null" being returned or something.

PPS. I have done this in python, but now need it working in C#.

  client.get('https://secure.somesite.com.au/api/products/getprice', params={
                'productCode': '{}'.format(code)
            }, headers=headers, timeout=60)

The cookie here is in a header.

Upvotes: 1

Views: 279

Answers (0)

Related Questions