微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[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 黄季冬

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐