Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of JavaScript and C # URI coding

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "JavaScript and C # URI coding Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Chaotic URI coding

There are three coding methods in JavaScript: escape, encodeURI, encodeURIComponent

Main coding methods in C #: HttpUtility.UrlEncode, Server.UrlEncode, Uri.EscapeUriString, Uri.EscapeDataString

JavaScript is good, only three, C# is mainly used in so many, not listed other codes (HTML), a lot of do not understand, do not understand the fear, fear will become bitter, this article will explain in detail how to encode URI in JavaScript and C# (Note: this article does not involve other codes).

Escape: not recommended

Reason: eacape is a method in BOM, which can only correctly encode ASCII symbols, while encodeURI and encodeURIComponent can encode all Unicode symbols. ECMAScript v3 opposes the use of this method and the application replaces it with decodeURI () and decodeURIComponent ().

There are 69 uncoded characters in escape: *, +, -,., /, @, _, 0-9 ~.

EncodeURI: used to encode the URL (no parameters)

There are 82 unencoded characters in encodeURI:!, #, $, &,', (,), *, +, +, -,., /,:;, =,?, @, _, ~, 0-9 PersonaHashin, Amurz.

That's what encodeURI is designed for. EncodeURI does not encode special characters in URI, such as colons (:), slashes (/). Here's an example:

EncodeURI ("http://www.cnblogs.com/a file with spaces.html") / / outputs http://www.cnblogs.com/a%20file%20with%20spaces.html

You can see that only 20% of the spaces have been replaced, so this method can be used to encode the URL.

Because encodeURI does not encode colons (:) and slashes (/), parsing errors occur if parameters (such as URL as parameters) contain colons (:) and slashes (/), so this method cannot encode parameters.

EncodeURIComponent: used to encode URL parameters

There are 71 unencoded characters in encodeURIComponent:!,', (,), *, -,., _, ~, 0-9.

You can see that this method encodes: / all, so you can't use it to encode the URL. Because this method encodes Chinese, space, pound sign (#), slash (/) and colon (:), it is suitable to encode the parameters in URI. Look at the following example:

Var param= "blog"; var url= "http://www.cnblogs.com/?key="+encodeURIComponent(param)+"&page=1"; console.log (url); / / outputs http://www.cnblogs.com/?key=%E5%8D%9A%E5%AE%A2%E5%9B%AD&page=1

As you can see, this is exactly what we want (only the parameters that need to be encoded are encoded here (page=1 does not need to be encoded).

Server.UrlEncode & & HttpUtility.UrlEncode: not recommended

Put the two together because the two methods are the same in most cases. The difference between them is that HttpUtility.UrlEncode uses UTF8 format encoding by default, while Server.UrlEncode uses system preset format encoding, and Server.UrlEncode uses system preset encoding as parameters to call HttpUtility.UrlEncode encoding, so if the system is encoded in UTF8 format globally, the two methods are the same.

How are these two methods coded? let's look at an example:

String url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b= blog # abc"; Response.Write (HttpUtility.UrlEncode (url1)); / / output http%3a%2f%2fwww.cnblogs.com%2fa+file+with+spaces.html%3fa%3d1%26b%3d%e5%8d%9a%e5%ae%a2%e5%9b%ad%23abc

As we can see from the above example, HttpUtility.UrlEncode encodes colons (:) and slashes (/), so it cannot be used to encode a URL.

So can we encode the parameters? the answer is also no. Because spaces should be encoded as% 20 in parameters rather than by HttpUtility.UrlEncode as a plus sign (+), it is not recommended to encode URI with these two methods.

Uri.EscapeUriString: used to encode the URL (no parameters)

Let's use examples:

String url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b= blog Park # abc"; Response.Write (Uri.EscapeUriString (url1)); / / outputs: http://www.cnblogs.com/a%20file%20with%20spaces.html?a=1&b=%E5%8D%9A%E5%AE%A2%E5%9B%AD#abc

As you can see, Uri.EscapeUriString encodes spaces and Chinese characters, but does not encode colons (:), slashes (/) and pound signs (#), so this method can be used to encode URLs, but not parameters, which is similar to the encodeURI method in JavaScript.

Uri.EscapeDataString: used to encode URL parameters

Still use examples to say:

String url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b= blog # abc"; Response.Write (Uri.EscapeDataString (url1)); / / outputs: http%3A%2F%2Fwww.cnblogs.com%2Fa%20file%20with%20spaces.html%3Fa%3D1%26b%3D%E5%8D%9A%E5%AE%A2%E5%9B%AD%23abc

As you can see, Uri.EscapeDataString encodes colons (:), slashes (/), spaces, Chinese, and pound signs (#), so this method cannot be used to encode URLs, but it can be used to encode parameters, similar to the encodeURIComponent method in JavaScript.

This is the end of the content of "JavaScript and C # URI coding Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report