如何在同一页面保持session
在我的页面加载中,我得到了会话[“name”]
当我使用此代码保存时:
Stream stream = null;
request = (HttpWebRequest)WebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
当涉及到这一行时:
response = (HttpWebResponse)request.GetResponse();
它再次移至页面加载,此时会话为空。如何???如何在同一页面保持会话。为什么当这一行遇到...时它被清除
in my pageload i have got the session["name"]
When i use this code to save:
Stream stream = null;
request = (HttpWebRequest)WebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
When it comes to this line:
response = (HttpWebResponse)request.GetResponse();
it again move on to the pageload and that time the session is null. how??? how to maintain the session in the same page. why it is cleared when this line encounters...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
会话不通过 HttpWebResponse 持续存在的原因是,默认情况下,
HttpWebResponse
不会为您处理 cookie。 ASP.NET 使用 cookie 来识别哪个会话属于用户。值得庆幸的是,有一个名为
CookieContainer
的帮助器类可以帮助您完成此任务。创建一个 CookieContainer 并将其附加到您的 Web 请求 - 在后续请求中,您将需要将 Cookie 容器或其中的 Cookie 再次附加到请求以保留会话:The reason that sessions don't persist with HttpWebResponse is because by default,
HttpWebResponse
will not handle cookies for you. ASP.NET uses a cookie to identify which session belongs to a user.Thankfully, there's a helper class called
CookieContainer
that can help you with this. Create aCookieContainer
and attach it to your web request - on subsequent requests, you will need to attach the cookie container, or the cookies within it to the request again to persist session:您尝试从您的应用程序发出 Web 请求,但它不是您的会话,而是您的应用程序会话。
数据(名称键及其值)存储在您的会话中,但是当您调用
WebRequest.GetResponse()
方法时,您的应用程序将启动它自己的全新会话。You are trying to make the Web Request from your application and right there it is not your session, but your application session.
The data (name key and it's value) is stored in your session, but when you call
WebRequest.GetResponse()
method then your application starts it's own, brand new session.