2012-03-09 115 views
2

我的应用程序假设连接一个web服务并激活他的一些功能。 首先,应用程序激活一个“登录”函数,该函数获取参数username和password,该函数在数据库中搜索用户名和密码,并在im登录时返回我。并创建一个会话瓦尔我喜欢:Android ksoap2会话cookie管理

Session["Username"] = User.Username; 

Session["FullName"] = User.FullName; 

更多...

,比我想主动另一个Web服务功能 - UpdateProfile 更改数据库在我的设定值。

所以,我的应用程序有一些私有类(asynctasks) 和每个asynctask负责web服务中的一个功能。

例如 - 登录的AsyncTask:

private class LoginAsyncTask extends AsyncTask<String, String, User> 
{ 

    private String METHODNAME = "Login"; 
    private String SOAPACTION = "http://tempuri.org/Login"; 

更多...

在此登陆的AsyncTask我解析正在添加的回饼干这样的:

cookies is a HashMap<String, String>(); 

    try 
    { 

     //respHeaders = trans.call(SOAPACTION, envelope, null); 
     reshttpHeaders = trans.call(SOAPACTION, envelope, null); 

    } 
    catch (Exception e) 
    { 
     //connection error. 
     e.printStackTrace(); 
     return null; 
    } 
    cookies.clear(); 
     if (reshttpHeaders!=null) { 
      for (int i = 0; i < reshttpHeaders.size(); i++) { 
       HeaderProperty hp = (HeaderProperty)reshttpHeaders.get(i); 
       String key = hp.getKey(); 
       String value = hp.getValue(); 
       if (key!=null && value!=null) { 
        if (key.equalsIgnoreCase("set-cookie")){ 
         String cookieString = value.substring(0,value.indexOf(";")); 

         cookies.put(cookieString.substring(0, cookieString.indexOf("=")),cookieString.substring(cookieString.indexOf("=")+1)); 
         break; 
        } 
       } 
      } 

     } 

比,在另一个asynctask叫UpdateProfileAsynctask 即时发送这个cookie,像这样:

 List<HeaderProperty> httpHeaders = new ArrayList<HeaderProperty>(); 
     for (String cookie:cookies.keySet()) { 
      httpHeaders.add(new HeaderProperty("Cookie", cookie + "=" + cookies.get(cookie))); 
     } 

     try 
     { 

      //trans.call(SOAPACTION, envelope, reqHeaders); 
      trans.call(SOAPACTION, envelope, httpHeaders); 
     } 

当我尝试用wireshark捕获这个数据包时,我发现我得到的cookie是: Set-Cookie:ASP.NET_SessionId = kmwn4l2qzc0k1anfk1du4ty1;路径= /;仅Http \ r \ n

和我的饼干,我送的是: 饼干:ASP.NET_SessionId = kmwn4l2qzc0k1anfk1du4ty1 \ r \ n

的问题是,Web服务不认识我(第二个要求是在20分钟期间)。

在web服务运行在这部分代码:

if (Session["Username"] == null) 
    return "Cant Update profile now, Your connection seems to be timeout"; 

,我得到这个消息的所有时间。但它的有时它的工作方式:/

谢谢。

回答

0

我在阅读完您的问题后解决了我的问题,谢谢。 我的代码就像下面这样:

HeaderProperty headerPropertySessionId = new HeaderProperty(“Cookie”,“key1 = value1”); List headerPropertyList = new ArrayList(); headerPropertyList.add(headerPropertySessionId);