怎样实现“当Android应用程序访问WEB服务器的时候,与服务器保持同一会话,也就是说当前登录用户与服务器的交互是在同一个Session下”网上给了好多例子都没有真正帮到忙,希望能给出asp.net服务器端和android端的代码,真心求教,初来乍到没有多少分,求帮忙。asp.netandroidsession

解决方案 »

  1.   

    要点在于,你的android需要接受来自服务器的全部set-cookie指令,并且在之后的请求中附加这些cookie,自然就保持会话了。
      

  2.   

    #2正解。只要能保留到Cookie(即接受Set-Cookie),且每次请求服务器均带上此Cookie,那就跟IE访问服务器是一样的。所以,如果你用Ajax,那要注意把Cookie保存下来;如果是用Java那在做web连接时,要保留CookieContainer了。
      

  3.   

    求代码,服务器端的最好也有android端的
      

  4.   

    服务器端用的是asp.net的mvc,在controller中写了个拦截规则,要求是android端登录之后也可以得到授权,服务器端拦截类[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
        public class AuthorizeFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                /*
                判断是否已经登录                       
                */
                LoginViewModel vm=filterContext.HttpContext.Session["CurrentUser"] as LoginViewModel;
                if (vm == null)
                {
                    HttpContext.Current.Response.RedirectToRoute(new { Controller = "Login", Action = "Index", Url = HttpContext.Current.Request.Url.ToString() });
                }
            }因为现在用了这个拦截类服务器端的所有接口android端都访问不了,求指教,谢谢了