你好我在1步中发送postRequest和postPesponse所有好的2步cookie都是空的....我在这里缺少什么?
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
这里播出的是我做新的重定向到页面之后的提示,但是当你看到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();
//]]>
答案 0 :(得分:3)
用于第二个请求
postRequest.CookieContainer = request.CookieContainer;
这将使用第一个请求的cookie。 否则,您必须在手动添加cookie时设置正确的URI http://msdn.microsoft.com/en-us/library/ckch3yd2(v=vs.110).aspx
答案 1 :(得分:0)
您可以尝试使用cookie的网络客户端,如下所示:
答案 2 :(得分:0)
尝试使用静态方法: 保存:
HttpContext.Current.Response.Cookies.Add["MyField"];
HttpContext.Current.Request.Cookies["MyField"];