本文介绍了如何通过HttpWebRequest正确地携带SessionID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试使用Session.SessionID来支持旧的sessionID。使用HttpWebRequest CookieContainer来获取所需的 页面会话变量,但它没有按预期工作。 HttpResponse对象总是从旧的 中返回一个不同的sessionID,我试图强制它。为什么objRequest没有携带 旧的SessionID? private String ReadHtmlPage(string url) { String result = string.Empty; CookieContainer myContainer = new CookieContainer(); HttpWebRequest objRequest = (HttpWebRequest)WebRequest .Create(url); objRequest.Method =" GET"; objRequest.UserAgent =" Mozilla / 4.0(兼容; MSIE 6.0; Windows NT5.0; .NET CLR 1.0.2914)" ;; objRequest.CookieContainer = new CookieContainer(); Cookie c = new Cookie( ); c.Name =" ASP.NET_SessionId"; c.Value = Session.SessionID; c.Domain =" ; http:// localhost / mysite /" ;; myContainer.Add(c); Response.Write(" OLD SessionID - " + Session.SessionID + "< br>"); HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); //保留饼干 foreach(饼干厨师在objResponse.Cookies) { Response.Write(" New SessionID - + cook.Name + cook.Value + cook.Domain +"< br>"); } //查看html。 使用(StreamReader sr = new StreamReader(objResponse.GetResponseStream()) ) { result = sr.ReadToEnd(); //关闭并清理StreamReader sr.Close(); } 返回结果; } 谢谢 Rod I am trying to foward the old sessionID using "Session.SessionID" toan HttpWebRequest CookieContainer so that I can capture the requestedpage session variables but it is not working as it is supposed to. TheHttpResponse object always returns a different sessionID from the oldone which I am trying to force. Why is objRequest not carrying over theold SessionID? private String ReadHtmlPage(string url){String result = string.Empty;CookieContainer myContainer = new CookieContainer();HttpWebRequest objRequest =(HttpWebRequest)WebRequest.Create(url);objRequest.Method = "GET";objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE6.0; Windows NT5.0; .NET CLR 1.0.2914)";objRequest.CookieContainer = new CookieContainer();Cookie c = new Cookie();c.Name = "ASP.NET_SessionId";c.Value = Session.SessionID;c.Domain = "http://localhost/mysite/";myContainer.Add(c);Response.Write("OLD SessionID -" + Session.SessionID +"<br>");HttpWebResponse objResponse =(HttpWebResponse)objRequest.GetResponse();//retain the cookiesforeach (Cookie cook in objResponse.Cookies){Response.Write("New SessionID -" + cook.Name +cook.Value + cook.Domain + "<br>");} //Check out the html.using (StreamReader sr =new StreamReader(objResponse.GetResponseStream())){result = sr.ReadToEnd(); // Close and clean up the StreamReadersr.Close();} return result;} ThanksRod 推荐答案 这篇关于如何通过HttpWebRequest正确地携带SessionID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 12:43