如何在 VB.NET 的 HTTPCookie 中使用 & 符号?

发布于 2024-08-13 04:34:49 字数 486 浏览 5 评论 0原文

我向用户保存了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2024-08-20 04:34:49

您可以使用 URLEncode 方法。

类似于:

imports HttpContext.Current
...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = Server.UrlEncode(FullSearchCriteria.SearchType)

这是至关重要的,因为 cookie 中只允许使用某些字符,其中包含诸如 & 符号破坏它们

You can use the URLEncode method.

Something like:

imports HttpContext.Current
...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = Server.UrlEncode(FullSearchCriteria.SearchType)

This is essential as only certain characters are allowed in cookies with characters such as ampersands breaking them.

dawn曙光 2024-08-20 04:34:49

噢!我真是个笨蛋...

Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchType)

D'oh! I'm such a dork...

Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchType)
失去的东西太少 2024-08-20 04:34:49

cookie 值需要进行编码。我不是VB专家,但看起来这是用这个方法完成的

System.Web.HttpUtility.UrlEncode

The cookie values need to be encoded. I'm no VB expert, but it looks like this is done with the method

System.Web.HttpUtility.UrlEncode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文