回发后编码损坏

发布于 2024-09-08 05:54:29 字数 644 浏览 3 评论 0原文

我有一个查询字符串,其参数值包含编码为 %e5 的挪威语字符 å。该页面包含一个带有 action 属性的表单,该属性由 ASP.Net 自动填充。当 URL 输出到所述属性时,它会以完整的两字节编码打印:%u00e5

当回发时,在调试后面的代码时这似乎没问题。但是,该页面实际上会重定向到自身(由于某些其他原因),并且重定向位置标头如下所示: Location: /myFolder/MyPage.aspx?Param1=%C3%A5

因此 %e5 已转换为 %C3%A5 ,这会以某种方式破坏输出。

在 HTML 文本中,通过 HttpUtility.HtmlEncode 输出后,损坏的字符看起来像 Ã¥

整个 Web 应用程序均采用 ISO8859-1 编码。

附言。在发布表单之前,从操作属性中的输出 %u00e5 中删除 u00 时,所有内容都会很好地输出。但错误似乎是从 %e5%C3%A5 的翻译。 (当然还有自我重定向,但那是另一回事。)

有什么指示吗?

I have a query string with a parameter value that contains the norwegian character å encoded as %e5. The page contains a form with an action attribute which is automatically filled by ASP.Net. When the URL is output into said attribute it is printed with a full two byte encoding: %u00e5.

When posting back this seems to be ok when debugging the code behind. However the page actually does a redirect to itself (for some other reason) and the redirect location header looks like this: Location: /myFolder/MyPage.aspx?Param1=%C3%A5

So the %e5 has been translated to %C3%A5 which breaks the output somehow.

In HTML text the broken characters look like å after having been output via HttpUtility.HtmlEncode.

The entire web application is ISO8859-1 encoded.

PS. When removing the u00 from the output %u00e5 in the action attribute before posting the form, everything is output nicely. But the error seems to be the translation from %e5 to %C3%A5. (And of course the self redirect, but that's another matter.)

Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

似最初 2024-09-15 05:54:29

我最终得到的解决方案是手动编码重定向 URL。

public void ReloadPage()
{
    UrlBuilder url = new UrlBuilder(Context, Request.Path);
    foreach (string queryParam in Request.QueryString.AllKeys)
    {
        string queryParamValue = Request.QueryString[queryParam];
        url.AddQueryItem(queryParam, queryParamValue);
    }
    Response.Redirect( url.ToString(), true);
}

url.AddQueryItem 基本上执行 HttpContext.Server.UrlDecode(queryParamValue) 操作,而 url.ToString 构建查询字符串,并为每个查询项执行 HttpContext.Server.UrlEncode(queryParamValue) 操作。

UrlBuilder 是我们库中已经存在的一个类,因此一旦我发现问题并意识到 C#/.Net 没有为此提供工具,就可以快速编写修复程序:)

The solution I ended up with was encoding the redirect URL manually.

public void ReloadPage()
{
    UrlBuilder url = new UrlBuilder(Context, Request.Path);
    foreach (string queryParam in Request.QueryString.AllKeys)
    {
        string queryParamValue = Request.QueryString[queryParam];
        url.AddQueryItem(queryParam, queryParamValue);
    }
    Response.Redirect( url.ToString(), true);
}

The url.AddQueryItem basically does HttpContext.Server.UrlDecode(queryParamValue) and the url.ToString builds the query string and for each query item does HttpContext.Server.UrlEncode( queryParamValue).

The UrlBuilder is a class already present in our library, so once I found the problem and realized that C#/.Net didn't provide tools for this, coding the fix was quick :)

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