C# Byte[] 到 Url 友好字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1886686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Byte[] to Url Friendly String
提问by LorenVS
I'm working on a quick captcha generator for a simple site I'm putting together, and I'm hoping to pass an encrypted key in the url of the page. I could probably do this as a query string parameter easy enough, but I'm hoping not too (just because nothing else runs off the query string)...
我正在为一个我正在整理的简单网站开发一个快速验证码生成器,我希望在页面的 url 中传递一个加密密钥。我可能可以很容易地将其作为查询字符串参数来执行,但我希望不会(只是因为查询字符串中没有其他任何内容)...
My encryption code produces a byte[], which is then transformed using Convert.ToBase64String(byte[]) into a string. This string, however, is still not quite url friendly, as it can contain things like '/' and '='. Does anyone know of a better function in the .NET framework to convert a byte array to a url friendly string?
我的加密代码生成一个字节 [],然后使用 Convert.ToBase64String(byte[]) 将其转换为字符串。然而,这个字符串对 url 仍然不太友好,因为它可以包含诸如“/”和“=”之类的内容。有谁知道 .NET 框架中有更好的函数可以将字节数组转换为 url 友好的字符串?
I know all about System.Web.HttpUtility.UrlEncode() and its equivalents, however, they only work properly with query string parameters. If I url encode an '=' inside of the path, my web server brings back a 400 Bad Request error.
我对 System.Web.HttpUtility.UrlEncode() 及其等价物了如指掌,但是,它们只能与查询字符串参数一起正常工作。如果我在路径内对 '=' 进行 url 编码,我的 Web 服务器会返回 400 Bad Request 错误。
Anyways, not a critical issue, but hoping someone can give me a nice solution
无论如何,不是一个关键问题,但希望有人能给我一个很好的解决方案
**EDIT: Just to be absolutely sure exactly what I'm doing with the string, I figured I would supply a little more information.
**编辑:为了绝对确定我对字符串做了什么,我想我会提供更多信息。
The byte[] that results from my encryption algorithm should be fed through some sort of algorithm to make it into a url friendly string. After this, it becomes the content of an XElement, which is then used as the source document for an XSLT transformation, and is used as a part of the href attribute for an anchor. I don't believe the xslt transformation is causing the issues, since what is coming through on the path appears to be an encoded query string parameter, but causes the HTTP 400
由我的加密算法产生的字节 [] 应该通过某种算法输入,使其成为一个 url 友好的字符串。此后,它成为 XElement 的内容,然后用作 XSLT 转换的源文档,并用作锚点的 href 属性的一部分。我不相信 xslt 转换会导致问题,因为路径上通过的似乎是编码的查询字符串参数,但会导致 HTTP 400
I've also tried HttpUtility.UrlPathEncode() on a base64 string, but that doesn't seem to do the trick either (I still end up with '/'s in my url)**
我也在 base64 字符串上尝试了 HttpUtility.UrlPathEncode() ,但这似乎也不起作用(我仍然在我的 url 中使用了“/”)**
采纳答案by orip
You're looking for HttpServerUtility.UrlTokenEncode
and HttpServerUtility.UrlTokenDecode
, in System.Web
.
您正在寻找HttpServerUtility.UrlTokenEncode
和HttpServerUtility.UrlTokenDecode
,在System.Web
。
They encode in base64, replacing the potentially dangerous '+' and '/' chars with '-' and '_' instead.
它们以 base64 编码,用“-”和“_”代替潜在危险的“+”和“/”字符。
回答by David Glenn
Have a look at System.BitConverter.ToString(myByteArray)
看看System.BitConverter.ToString(myByteArray)
Handy for one way encoding for things like hashes but as pointed out by ssg it's not very efficient. I wouldn't recommend it for large amounts of data.
对于像散列这样的东西的一种编码方式很方便,但正如 ssg 所指出的那样,它不是很有效。我不会推荐它用于大量数据。