On the transcoding of web data transfer

Source: Internet
Author: User

Between the browser and the server, the transfer of data, will involve transcoding problems, this article describes the specific situation.

One, type

There are three types of transmission that will have transcoding problems (WebSocket does not have this problem), including cookies and get,post.

A, cookies

Something in the client cookie will not be transcoded, but once transferred to the server, it will be automatically encodeuricomponent, the server resolves it once, and therefore is restored, but the value in it cannot have ";", otherwise the cookie will be wrong, or Chinese, Of course, if you want to pass these, you call encodeURIComponent when you assign a value, and then the server calls decode once.

b, get, and post

Get and post things are not transcoded (in fact, according to the settings, set the transcoding is only the entire content transcoding, rather than single value transcoding, still facing the value of the problem with special characters), so if not transcoding and may cause non-conformance? key=value&key2= value2 format, so you need to note it yourself, otherwise the server may not be recognized.

Second, the overweight and decoding

A, Escape&unescape

Most data will be transcoded, but not included in the-_.! ~* ' () "as well as numbers and letters, others will be converted to%XX or%uxxxx mode. But is not recommended, has been standard obsolete

B, Encodeuri&decodeuri

No transcoding "-_.! ~* ' () "also does not transcode";/?:@&=+$,# "and digital letters, others are transcoded to%xx. Used in the address bar because he does not transcode any characters associated with the URL, such as http://www.xxx.com/default.html?abc=value&def=value2#, will be completely preserved, but note the need to transcode the contents of value , it may not be well-formed, and note that value cannot be transcoded with encodeURI, otherwise such as + will not be transcoded, but submission to the server will be a space, also such as & will not, will cause server recognition error.

C, encodeuricomponent&decodeuricomponent

And encodeURI The difference is will transcode ";/?:@&=+$,#", the other no difference. It is characterized by the ability to support transcoding content, so that the content that needs to be transferred does not affect the address of the URI or the transmitted data format, used for pure data, but cannot be used to transcode the address, otherwise the address must be decoded once, or cannot be accessed. http://www.xxx.com/default.html?abc=value&def=value2# will be transcoded to http%3a%2f%2fwww.xxx.com%2fdefault.html%3fabc% 3dvalue%26def%3dvalue2%23.

Third, server (ASP)

The data received by the server will perform the necessary decoding work, and the use of UrlDecode (that is, all decoding, corresponding to the Encodeuricomponent,encodeuri corresponds to Urlpathdecode), but note that some symbols will be special transcoding, such as "+" will be converted to "" "Space," & "will be treated as a delimiter, which is why the client needs to transcode the value once, otherwise the symbol without transcoding will cause some error results.

Iv. Summary

The result is that if you need to communicate between the data and the server (Get&post), it is recommended that all the data content (that is, the value content, without the key, of course, it is OK, anyway key is usually the content of the specification, but not to "="), encodeURIComponent, the server received after the read is already decoded, do not need to be solved again, while the browser will automatically transcode cookies, and the server will not decode, need to solve their own.

1, the browser to send, you need to get&post all the value of transcoding, the cookie will turn itself;

2, when the server receives, the cookie, get and post are decoded once;

3, the browser does not need any decoding when receiving, and the server sent the same, the same way, the server sent no need to transcode, unless special needs;

4, Ajax, if the pass is a form, you can set its properties to confirm whether to let its help transcoding (enctype= "Multipart/form-data"), but it does not make any sense, only to distinguish between text or file transfer, see/HTTP Www.cnblogs.com/21tcy/p/4814673.html.

On the transcoding of web data transfer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.