无法解码网址以更正值

时间:2019-08-20 12:44:24

标签: c# urlencode urldecode

以下代码:

context.Response.Write(
    context.Request.Url.AbsolutePath
    + "\n" + Uri.UnescapeDataString(context.Request.Url.AbsolutePath)
    + "\n" + context.Server.UrlDecode(context.Request.Url.AbsolutePath)
    + "\n" + HttpUtility.UrlDecode(context.Request.Url.AbsolutePath)
);

输出:

/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera%20hp%20-%20c%C3%B3pia%20(2).png
/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera hp - cópia (2).png
/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera hp - cópia (2).png
/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera hp - cópia (2).png

使用this one之类的工具并输入/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera%20hp%20-%20c%C3%B3pia%20(2).png并按解码,结果如下:

/uploads/324/d7925fa2-2d3a-4cae-ba3d-e22f48cbe55c/c/1415663111_250/regenera hp - cópia (2).png

为什么我在尝试进行URL解码时未得到正确的值?

2 个答案:

答案 0 :(得分:1)

HttpUtility.Decode有一个重载,可让您指定编码。您需要匹配使用该重载对URL最初进行编码的编码。

在您的情况下,您似乎需要UTF8:-

var decoded = HttpUtility.UrlDecode(url, Encoding.UTF8);

答案 1 :(得分:0)

尝试一下:

System.Net.WebUtility.UrlDecode(context.Request.Url.AbsolutePath);

enter image description here