我的應用程序假設連接一個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";
,我得到這個消息的所有時間。但它的有時它的工作方式:/
謝謝。