在C#中解开一个转义的url [英] Unescape an escaped url in c#

查看:155
本文介绍了在C#中解开一个转义的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我有以下格式转义的网址:

  http://www.someurl.com/profile .php?mode = register& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; / p> 

  http://www.someurl.com/profile.php?mode=register&agreed=true 

这是与escapped html相同的东西吗?



这样做?



谢谢

解决方案

& amp; amp;< / code>是一个HTML实体,并且当文本被编码为HTML时使用,因为您必须转义具有以下内容的& HTML中的特殊含义。显然,这种转义机制被用在URL上,大概是因为它在一些HTML中被使用,例如在一个链接中。我不知道为什么要解码它,因为浏览器将在点击链接时进行正确的解码。但是无论如何,要恢复它,您可以在 System.Web 命名空间中使用 HttpUtility.HtmlDecode

  var encoded =http://www.someurl.com/profile.php?mode=register&amp;agreed=true; 
var decoding = HttpUtility.HtmlDecode(encoded);

解码的值是: p>

  http://www.someurl.com/profile.php?mode=register&agreed=true 

使用的另一种形式的编码/解码是URL编码。这被用来能够在URL的一部分中包含特殊字符。例如,角色 / & URL中的特殊含义。如果您需要在说出查询参数中包含任何这些字符,则必须对该参数进行URL编码,以免弄乱该URL。以下是使用URL转义的URL的示例:

  http://www.someurl.com/profile。 php?company = Barnes +%26 + Noble 

公司名称 Barnes&贵族编码为 Barnes +%26 + Noble 。如果& 未被转义,则URL不会包含两个查询参数,因为& 用作查询参数之间的分隔符。


Possible Duplicate:
How can I decode HTML characters in C#?

I have urls which is escaped in this form:

   http://www.someurl.com/profile.php?mode=register&amp;agreed=true

I want to convert it to unescaped form

   http://www.someurl.com/profile.php?mode=register&agreed=true

is this the same thing as escapped html?

how do i do this?

thanks

解决方案

&amp; is an HTML entity and is used when text is encoded into HTML because you have to "escape" the & that has a special meaning in HTML. Apparently, this escaping mechanism was used on the URL presumably because it is used in some HTML for instance in a link. I'm not sure why you want to decode it because the browser will do the proper decoding when the link is clicked. But anyway, to revert it you can use HttpUtility.HtmlDecode in the System.Web namespace:

var encoded = "http://www.someurl.com/profile.php?mode=register&amp;agreed=true";
var decoded = HttpUtility.HtmlDecode(encoded);

The value of decoded is:

http://www.someurl.com/profile.php?mode=register&agreed=true

Another form of encoding/decoding used is URL encoding. This is used to be able to include special characters in parts of the URL. For instance the characters /, ? and & have a special meaning in a URL. If you need to include any of these characters in a say a query parameter you will have to URL encode the parameter to not mess up the URL. Here is an example of an URL where URL escaping has been used:

http://www.someurl.com/profile.php?company=Barnes+%26+Noble

The company name Barnes & Noble was encoded as Barnes+%26+Noble. If the & hadn't been escaped the URL would have contained not one but two query parameters because & is used as a delimiter between query parameters.

这篇关于在C#中解开一个转义的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆