[CF.Skills]在.NET Compact Framework中设置webservice的session状态

.NET CF中没有Cookie Container之类方便管理cookie的类,我刚刚简单的搜索了一下,找到一段用作Session管理器的代码:(原文见 这里)
         /// <summary>
        
/// This field contains the received session cookie
        
/// </summary>

         private   string  cookie  =   null ;

        
/// <summary>
        
/// Http-Header for the request SessionCookie
        
/// </summary>

         private   const   string  REQUESTHEADER_SESSIONCOOKIE  =   " Cookie " ;

        
/// <summary>
        
/// Http-header for the response SessionCookie
        
/// </summary>

         private   const   string  RESPONSEHEADER_SESSIONCOOKIE  =   " Set-Cookie " ;

        
private   void  ProcessResponse(System.Net.HttpWebResponse response)
        
{
            
// Is the cookie present in the response?
            if (response.Headers.Get(RESPONSEHEADER_SESSIONCOOKIE) != null)
            

                
// Store the cookie
                cookie = response.Headers.Get(RESPONSEHEADER_SESSIONCOOKIE);
            }

        }


        
/// <summary>
        
/// This override will tweak the request to allow Session-cookies
        
/// </summary>
        
/// <returns>The tweaked request</returns>

         protected   override  System.Net.WebRequest GetWebRequest(Uri uri)
        
{
            System.Net.HttpWebRequest request 
= (System.Net.HttpWebRequest)base.GetWebRequest(uri);

            
// Is the session cookie cached?
            if (cookie != null)
            

                
// Add the sessioncookie to the request
                request.Headers.Add(REQUESTHEADER_SESSIONCOOKIE, cookie);
            }

            
return request;
        }


        
/// <summary>
        
/// This override will tweak the response to allow Session-cookies
        
/// </summary>

          protected   override  System.Net.WebResponse GetWebResponse(System.Net.WebRequest request)
        
{
            System.Net.HttpWebResponse response 
= (System.Net.HttpWebResponse)base.GetWebResponse(request);
            ProcessResponse(response);
            
return response;
        }


        
/// <summary>
        
/// This override will tweak the response to allow Session-cookies
        
/// </summary>

         protected   override  System.Net.WebResponse GetWebResponse(System.Net.WebRequest request, IAsyncResult result)
        
{
            System.Net.HttpWebResponse response 
= (System.Net.HttpWebResponse)base.GetWebResponse(request, result);
            ProcessResponse(response);
            
return response;
        }

除了这种人工控制请求应答的消息头的方式,不知道在.NET CF中大家还有哪些方案来维系webservice的会话,不妨一同探讨一下:-)

Regards

黄季冬
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值