简体   繁体   English

HttpWebRequest不接受Cookie C#

[英]HttpWebRequest no accepting cookies C#

I'm having an issue with HttpWebRequest accepting cookies, this is my code: 我在HttpWebRequest接受Cookie时遇到问题,这是我的代码:

        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. 抱歉...我们检测到您的浏览器未设置为允许会话Cookie。 Our platform uses cookies to help enhance your overall user experience. 我们的平台使用cookie来帮助增强您的整体用户体验。 You cannot log in without them. 没有他们,您将无法登录。 Please enable Session Cookies and try again. 请启用会话Cookie,然后重试。 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. 当我尝试自动执行该过程时,问题就来了。

Sounds like the url you're talking to is expecting some specific headers. 听起来您正在与之交谈的url需要一些特定的标头。 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). 尝试将User-Agent标头设置为Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0 (默认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. 失败的话,请查看Firefox中的请求(打开开发人员工具,然后在按下登录按钮之前切换到“网络”标签),然后查看该请求还有其他标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM