user3458958
user3458958

Reputation:

Cookies is empty after response

Hello i sending postRequest and postPesponse in 1 step all good in 2 step the cookies is empty....What i am missing here?

                    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

Sow here is the promleb after this i doing the new redirect to page but as you see cookies is empty and i get stack.Any ideas?
I just find out that in postResponseAfterLogin i have this function in side of code with my link that i need and timeOut(2000).This can help to resolve this problem?

<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();
//]]>

Upvotes: 0

Views: 666

Answers (3)

user2964420
user2964420

Reputation: 148

Use for the second request

postRequest.CookieContainer = request.CookieContainer;

This will use the cookies of the first request. Otherwise, you must set the right URI when you add the cookies manually http://msdn.microsoft.com/en-us/library/ckch3yd2(v=vs.110).aspx

Upvotes: 3

Dgan
Dgan

Reputation: 10275

Try using Static Methods: For save:

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


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

Upvotes: 0

Venkat
Venkat

Reputation: 115

You can try the cookie aware web client like the below one:

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

Upvotes: 0

Related Questions