在web开发页面中调用web服务,默认情况下web服务收不到客户端的cookie,如果需要cookie,可以通过给webservice实例的CookieContainer属性赋值。
如下代码片段:
01 |
static void AddCookie( ref BBS.Services.UpgradeService service) |
03 |
CookieContainer cookies = new System.Net.CookieContainer(); |
04 |
string cookieDomain = new Uri(service.Url).Host; |
05 |
foreach ( string cookieKey in HttpContext.Current.Request.Cookies.AllKeys) |
07 |
System.Net.Cookie netCookie = new System.Net.Cookie(cookieKey, HttpContext.Current.Request.Cookies[cookieKey].Value, "/" , cookieDomain); |
08 |
netCookie.Expires = DateTime.Now.AddMinutes(5); |
09 |
cookies.Add(netCookie); |
12 |
service.CookieContainer = cookies; |
需要注意的是必须给netCookie设置过期时间,否则cookie传不到web服务中。