.NET Compact Framework在调用Axis实现的WebService中如何维持Session
最近在一个WinCE的设备上开发应用,通过GPRS上网调用WebService。服务端用java开发的,使用Axis的实现标准。发现.NET Compact Framework没有实现System.Net.CookieContainer,没法在WinCE的客户端保持会话。
后来通过分析Web请求响应的Http消息头,了解到Axis的Session是通过Cookie来管理,并通过在Http消息头中加入Cookie:JSESSIONID=XS52V1MBNVOAAQSNDBESKHA 进行传递的。在确定了问题原因之后,解决方案就比较简单了:
1、调用login成功的时候服务器返回SessionId;
2、截获HttpWebRequest,加入该消息头。代码如下:
--托管Web服务客户端代理--
1using System;
2using System.Net;
3using MyTest.DataTransfer.WebReference;
4
5namespace MyTest.DataTransfer
6{
7 /// <summary>
8 /// 数据传输适配器,用来托管web service接口.
9 /// </summary>
10 public sealed class TransmitAdapter : MyTestService
11 {
12 private string _jSessionID = string.Empty;
13 private static TransmitAdapter _instance;
14 private TransmitAdapter() : base()
15 {
16 }
17
18 //FIXUP: An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll?
19 protected override WebRequest GetWebRequest(Uri uri)
20 {
21 HttpWebRequest httpWebReq = base.GetWebRequest(uri) as HttpWebRequest;
22 if (!string.IsNullOrEmpty(_jSessionID))
23 {
24 httpWebReq.Headers.Add("COOKIE", "JSESSIONID=" + _jSessionID);
25 }
26 httpWebReq.AllowWriteStreamBuffering = true;
27 return httpWebReq;
28 }
29
30 /// <summary>
31 /// singleton.
32 /// </summary>
33 public static TransmitAdapter Instance
34 {
35 get
36 {
37 if (_instance == null)
38 {
39 _instance = new TransmitAdapter();
40 }
41 return _instance;
42 }
43 }
44
45 /// <summary>
46 /// Session Id.
47 /// </summary>
48 public string SessionID
49 {
50 get { return _jSessionID; }
51 set { _jSessionID = value; }
52 }
53 }
54}
2using System.Net;
3using MyTest.DataTransfer.WebReference;
4
5namespace MyTest.DataTransfer
6{
7 /// <summary>
8 /// 数据传输适配器,用来托管web service接口.
9 /// </summary>
10 public sealed class TransmitAdapter : MyTestService
11 {
12 private string _jSessionID = string.Empty;
13 private static TransmitAdapter _instance;
14 private TransmitAdapter() : base()
15 {
16 }
17
18 //FIXUP: An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll?
19 protected override WebRequest GetWebRequest(Uri uri)
20 {
21 HttpWebRequest httpWebReq = base.GetWebRequest(uri) as HttpWebRequest;
22 if (!string.IsNullOrEmpty(_jSessionID))
23 {
24 httpWebReq.Headers.Add("COOKIE", "JSESSIONID=" + _jSessionID);
25 }
26 httpWebReq.AllowWriteStreamBuffering = true;
27 return httpWebReq;
28 }
29
30 /// <summary>
31 /// singleton.
32 /// </summary>
33 public static TransmitAdapter Instance
34 {
35 get
36 {
37 if (_instance == null)
38 {
39 _instance = new TransmitAdapter();
40 }
41 return _instance;
42 }
43 }
44
45 /// <summary>
46 /// Session Id.
47 /// </summary>
48 public string SessionID
49 {
50 get { return _jSessionID; }
51 set { _jSessionID = value; }
52 }
53 }
54}
--调用Web服务--
string sessionId = MyTest.DataTransfer.TransmitAdapter.Instance.login(userName, password);
if (string.IsNullOrEmpty(sessionId))
{
//失败.
}
MyTest.DataTransfer.TransmitAdapter.Instance.SessionID = sessionId;
// OK?
bool isAvaiable =MyTest.DataTransfer.TransmitAdapter.Instance.checkSession();
if (string.IsNullOrEmpty(sessionId))
{
//失败.
}
MyTest.DataTransfer.TransmitAdapter.Instance.SessionID = sessionId;
// OK?
bool isAvaiable =MyTest.DataTransfer.TransmitAdapter.Instance.checkSession();
参考资源:
Session Management in ASP.NET
Thin Client for Microsoft Visual SourceSafe on Pocket PC / SmartPhones