如何将多个Cookie添加到WCF / REST服务响应 [英] How to add multiple cookies to Response in WCF / REST service
本文介绍了如何将多个Cookie添加到WCF / REST服务响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有机会获得 WebOperationContext
键,可以通过执行此添加一个cookie的:
I have access to WebOperationContext
and can add one cookie by doing this:
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a");
但是,如果我调用了几次,例如:
However if I call that several times, e.g.:
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_b=bar_b");
我应该得到以下在我的头(2饼干):
I should get the following in my header (2 cookies):
Set-Cookie: foo_a=bar_a
Set-Cookie: foo_b=bar_b
但是,相反得到:
Set-Cookie: foo_a=bar_a, foo_b=bar_b
如何设置多个Cookie? THX
How do I set multiple cookies? Thx
推荐答案
原来,Cookie可以在一个设置Cookie进行设置:标题行,但你需要放置一个 ;分号饼干...
Turns out the cookies can be set on one Set-Cookie: header line, but you will need to place a ';' semicolon at the end of the cookie...
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a;,"
+ foo_b=bar_b;");
再结果将是:(哪个浏览器认为是2饼干不只是1)
then the result will be: (which browser consider to be 2 cookies not just 1)
Set-Cookie: foo_a=bar_a;, foo_b=bar_b;
// browser sees this as 2 cookies: `foo_a` & `foo_b`
而不是
Set-Cookie: foo_a=bar_a, foo_b=bar_b
// browser sees this as 1 cookie: `foo_a` with value: `=bar_a, foo_b=bar_b`
这篇关于如何将多个Cookie添加到WCF / REST服务响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文