日期:2014-05-17  浏览次数:20395 次

web service中的状态保持问题
第一个方法中登陆成功后保存一个GUID的值在SESSION中,第二个方法取回这个值,在.asmx页面中是可以取回的,但到客户端工具中就无法取到,请问大侠们是怎么会事. 
我现在调试的客户端是C#的,但正式发布后是给JAVA调用的,请问: 
1. 我的c#的客户端如何才能取得保存的状态值呢. 
2. 如何才能让JAVA也能取得保存的状态值呢.  
  [WebMethod(EnableSession=true)] 
  public string Login(string name,string password) 
  { 
     ...... 
  //login successful 
  Guid currentUser = Guid.NewGuid(); 
  Session[ "currentUser "] = currentUser.ToString() 
   
  } 

  [WebMethod(EnableSession = true)] 
  public string GetGUID() 
  { 
  string currentUser = Session[ "currentUser "].ToString(); 
  return currentUser; 
  }

------解决方案--------------------
System.Net.CookieContainer()来获得保存的SESSION值
这里需要注意,并不是cookie来保存sesssion的值,session的管理是在服务器端,cookie只是用来区分用户的。
也就是为什么要在webservice中,加上上面一句,而web调用的时候不需要的道理
明白了这点,在java调用的时候,只需要模拟出来cookie就可以