Cookie 在响应后为空

本文关键字:响应 Cookie | 更新日期: 2023-09-27 17:55:45

你好,我在 1 步中发送 postRequest 和 postPesponse 在 2 步中都很好,饼干是空的......我在这里错过了什么?

                    CookieCollection cookies = new CookieCollection();
                    CookieCollection cookiesAfterLogin = new CookieCollection();
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginGetUrl);
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                    //Get the response from the server and save the cookies from the first request..
                    1step       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    cookies = response.Cookies;

                    Stream streamResponseLogin = response.GetResponseStream();
                    StreamReader streamReadLogin = new StreamReader(streamResponseLogin);
                    LoginInfo = streamReadLogin.ReadToEnd();
                    string postData = null;
                    postData += "__EVENTARGUMENT=" + GetValueByID(LoginInfo, "__EVENTARGUMENT") + "&";//The new
                    postData += "__REQUESTDIGEST=" + GetValueByID(LoginInfo, "__REQUESTDIGEST") + "&";
                    postData += "__VIEWSTATE=" + GetValueByID(LoginInfo, "__VIEWSTATE") + "&";
                    postData += "__EVENTVALIDATION=" + GetValueByID(LoginInfo, "__EVENTVALIDATION") + "&";
                    postData += "homeLogin$txtUsername=xx&";
                    postData += "homeLogin$txtPassword=xxx&";
                    postData += "__EVENTTARGET=homeLogin$connectLb";
                    HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(loginPostUrl);
                    postRequest.CookieContainer = new CookieContainer();
                    // Add the received Cookies from the HTTP Get
                    postRequest.CookieContainer.Add(cookies);
                    postRequest.Method = WebRequestMethods.Http.Post;
                    postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
                    postRequest.AllowWriteStreamBuffering = false;
                    postRequest.ProtocolVersion = HttpVersion.Version11;
                    postRequest.AllowAutoRedirect = false;
                    postRequest.ContentType = "application/x-www-form-urlencoded";
                    byte[] byteArray = Encoding.ASCII.GetBytes(postData);
                    postRequest.ContentLength = byteArray.Length;
                    Stream newStream = postRequest.GetRequestStream(); //open connection
                    newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
                    newStream.Close();
                    2step     HttpWebResponse postResponseAfterLogin = (HttpWebResponse)postRequest.GetResponse();
                    cookiesAfterLogin = postResponseAfterLogin.Cookies;
                    //ANd here --->cookiesAfterLogin.Count is 0

在这里播种是 promleb 在此之后,我执行新的重定向到页面,但正如您所看到的 cookie 是空的,我得到了堆栈。有什么想法吗?
我只是发现在postResponseAfterLogin中,我在代码旁边有这个函数,我需要的链接和timeOut(2000)。这可以帮助解决此问题吗?

<script type="text/javascript">
//<![CDATA[
 getLoader_Side(); function loginRedirect() { setTimeout("UpdateLoaderImg()", 50); top.location.href ="https://services.test.com/Pages/Trans.aspx";} setTimeout("loginRedirect()", 2000); var _spFormDigestRefreshInterval = 1440000;Sys.Application.initialize();
//]]>

Cookie 在响应后为空

用于

第二个请求

postRequest.CookieContainer = request.CookieContainer;

这将使用第一个请求的 cookie。否则,您必须在手动添加 Cookie 时设置正确的 URIhttp://msdn.microsoft.com/en-us/library/ckch3yd2(v=vs.110).aspx

您可以尝试使用cookie感知Web客户端,如下所示:

https://gist.github.com/paigecook/5221158

尝试使用静态方法:保存:

HttpContext.Current.Response.Cookies.Add["MyField"];

HttpContext.Current.Request.Cookies["MyField"];