session丢失问题

梦醒实分1 2010-06-14 01:05:01
我用httprequest请求指定页面,然后用httpresponse获取返回数据,然而返回数据居然是登陆页面,检查发现请求过程中session丢失,导致请求页面验证session不成功而跳转登陆页面,这个问题困扰死我了,请大家帮帮忙,指点下小弟。
...全文
116 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
可以使用asp.net状态服务或者用数据库保存状态的。在要不就自己想一个状态保存机制,估计比较复杂。
一般使用asp.net状态服务就够了
梦醒实分1 2010-06-14
  • 打赏
  • 举报
回复
感谢几位的指点,谢谢各位,我想干脆还是用cookie代替session好了,免得麻烦
人工智能算法 2010-06-14
  • 打赏
  • 举报
回复
一般来说 asp.net服务器都会在客户端cookie中存储一个临时cookie 当浏览器关闭则过期
这个值的key是"ASP.NET_SessionId" 存储了当前与服务器Session.SessionID相同的值

"ASP.NET_SessionId" 默认情况下禁止客户端脚本访问,只允许服务器访问.
它是客户端与服务器连接的凭据, 每次请求都必须包含一个cookie其中包括了请求的服务器的
Session.SessionID


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//请注意下面这条语句,它将开启HttpWebRequest的cookie接收功能
request.CookieContainer = new CookieContainer();

HttpWebResponse response = (HttpWebResponse) request.GetResponse();



// Print the properties of each cookie.
foreach (Cookie cook in response.Cookies)
{
Console.WriteLine("Cookie:");
Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
Console.WriteLine("Domain: {0}", cook.Domain);
Console.WriteLine("Path: {0}", cook.Path);
Console.WriteLine("Port: {0}", cook.Port);
Console.WriteLine("Secure: {0}", cook.Secure);

Console.WriteLine("When issued: {0}", cook.TimeStamp);
Console.WriteLine("Expires: {0} (expired? {1})",
cook.Expires, cook.Expired);
Console.WriteLine("Don't save: {0}", cook.Discard);
Console.WriteLine("Comment: {0}", cook.Comment);
Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
Console.WriteLine("Version: RFC {0}" , cook.Version == 1 ? "2109" : "2965");

// Show the string representation of the cookie.
Console.WriteLine ("String: {0}", cook.ToString());
}

//记得保存每次包含SessionID的cookie 在下次请求时 在Request.Cookies中包含此cookie
人工智能算法 2010-06-14
  • 打赏
  • 举报
回复
你使用的一个页面访问另一个页面 自然不会有session啊

当前页面是iis创建的进程,iis非browser无法于http服务器创建会话.并且不接受cooike
梦醒实分1 2010-06-14
  • 打赏
  • 举报
回复
除了配置config,还有没有别的方法,防止session丢失,这样吧,我贴上我的代码,请大家帮忙看下

//获取http响应数据
public string GetHttpResponse(string url)
{
string content = "";
//其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的Create方法来创建.
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest1.Method = "POST";
myHttpWebRequest1.ContentType = "application/x-www-form-urlencoded";
//不维持与服务器的请求状态
myHttpWebRequest1.KeepAlive = false;
myHttpWebRequest1.AllowAutoRedirect = false;
//创建一个HttpWebRequest对象
//Assign the response object of HttpWebRequest to a HttpWebResponse variable.\
HttpWebResponse myHttpWebResponse1;
try
{
myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
//设置页面的编码模式
System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
Stream streamResponse = myHttpWebResponse1.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse, utf8);

Char[] readBuff = new Char[256];
//这里使用了StreamReader的Read()方法,参数意指从0开始读取256个char到readByff中.
//Read()方法返回值为指定的字符串数组,当达到文件或流的末尾使,方法返回0
int count = streamRead.Read(readBuff, 0, 256);
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
content += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
myHttpWebResponse1.Close();
return content;
}
catch (WebException ex)
{
content = "在请求URL为:" + url.ToString() + " 的页面时产生错误,错误信息为" + ex.ToString();
return content;
}
}

调用: string str = GetHttpResponse("adseries_http.aspx?type=insert&value=1231");
Response.Write(str);

adseries_http.aspx页面

if (Session["Id"] != "")
{
Uri uri = Request.Url;
string url = uri.AbsoluteUri; //获取请求的URL
Response.Write(url);
}

session丢失,返回数据是登陆页面数据而非url
healer_kx 2010-06-14
  • 打赏
  • 举报
回复
。。。 。。。
学习一下。
快乐乔巴 2010-06-14
  • 打赏
  • 举报
回复
http://www.cnblogs.com/nick-fbx/archive/2008/11/14/1333244.html
老问题了 自己去看吧 懒得写了

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧