2

I'm having an issue with HttpWebRequest accepting cookies, this is my code:

        HttpWebRequest req= (HttpWebRequest)WebRequest.Create("https://www.companyabc.com/security?action=authenticate");
        req.CookieContainer = new CookieContainer();
        req.CookieContainer.Add(new Uri("https://www.companyabc.com"), new CookieCollection());
        string postData = "account_id=xxxx&password=xxxx";
        req.KeepAlive = true;

        byte[] send = Encoding.Default.GetBytes(postData);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = send.Length;

        Stream sout = req.GetRequestStream();
        sout.Write(send, 0, send.Length);
        sout.Flush();
        sout.Close();

The response I'm getting is:

Sorry... We have detected that your browser is not set up to allow Session Cookies. Our platform uses cookies to help enhance your overall user experience. You cannot log in without them. Please enable Session Cookies and try again. Contact us at ....

What am I doing wrong? Thank you in advanced.

FYI, I can access the web page and login without any issues from a browser. The issue comes when I try to automate the process.

John Smith
  • 41
  • 1
  • 10
  • 1
    the `error message` is telling you exactly what you need to do @JohnSmith you need to go to the options in your browser and enable Cookies.. if you need a step-by-step do a google search on the following `MSDN Enable Cookies` a simple `google search` would yield the following [MSDN HttpWebRequest.CookieContainer example](https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer%28v=vs.110%29.aspx) – MethodMan Feb 03 '15 at 23:20
  • Just wanted to write that down XD – EugenSunic Feb 03 '15 at 23:21
  • @MethodMan What "browser" are you taking about? And the OP appears to be doing exactly what the link you are telling him to follow does. – mason Feb 03 '15 at 23:41
  • @mason `We have detected that your browser is not set up to allow Session Cookies.` exactly what his error msg says ..that's where – MethodMan Feb 03 '15 at 23:43
  • 3
    Yes, but he is not using a browser. This is code. And he already was doing what you told him to do. I don't think your comment is adding anything to the discussion. – mason Feb 03 '15 at 23:44

1 Answers1

0

Sounds like the url you're talking to is expecting some specific headers. Try setting the User-Agent header to Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0 (the default Firefox UserAgent).

Failing that, have a look at the request in Firefox (open the developer tools, and switch to the 'Network' tab before you press the login button) and see what other headers the request has.