如何在 VB.NET 的 HTTPCookie 中使用 & 符号?
我向用户保存了一个 cookie,如下所示...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = FullSearchCriteria.SearchText
searchCookie.Item("SearchType") = FullSearchCriteria.SearchType
SearchText 存储他们在上一页中输入的值。我们观察到,如果 cookie 中存在“&”符号(例如 Tyne & Wear),则 cookie 不会保存后续值(SearchType)。
所发生的情况是 cookie 的输出如下:
SearchText=Tyne &
显然 & 符号混淆了 cookie。有办法防止这种情况发生吗?
I have a cookie saved to the user as follows...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = FullSearchCriteria.SearchText
searchCookie.Item("SearchType") = FullSearchCriteria.SearchType
The SearchText stores a value they have input in a previous page. We have observed if there is an ampersand in the cookie (eg Tyne & Wear), then the cookie doesn't save subsequent values (SearchType).
What happens is the cookie is output like this:
SearchText=Tyne &
Obviously the ampersand is confusing the cookie. Is there a way to prevent this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 URLEncode 方法。
类似于:
这是至关重要的,因为 cookie 中只允许使用某些字符,其中包含诸如 & 符号破坏它们。
You can use the URLEncode method.
Something like:
This is essential as only certain characters are allowed in cookies with characters such as ampersands breaking them.
噢!我真是个笨蛋...
D'oh! I'm such a dork...
cookie 值需要进行编码。我不是VB专家,但看起来这是用这个方法完成的
The cookie values need to be encoded. I'm no VB expert, but it looks like this is done with the method