Question about URL Chinese parameters of encodeuri

Source: Internet
Author: User

 

**************************************** ****************************
*Copyright Notice
*
* This document uses the Creative Commons release. Please strictly abide by this authorization protocol.
* This article was first published onBlogThis statement is an integral part of this article.
* Author's network name: prodigal son
* Author email: Dayichen (AT) 163.com
* Author's blog: Http: // www. cnblogs. com/walkingboy
*
**************************************** ****************************

Question about URL Chinese parameters of encodeuri

-Written by prodigal son @ cnblogs.com (07-01-26)

Abstract:

We pass parameters between pages. The most common method is to pass parameters through URLs. If Chinese or other special characters are involved, encodeuri is usually used for encoding on the client side, the server directly uses querystring [] to get the value (Asp.net will automatically help us to decode it), but it cannot be expected that this will also cause an error. Is it found that the form action address is inconsistent with the URL address? Asp.net seems to have performed some encoding when setting action for form (I still don't know )......

 

I. client-side querystring:

Since callbackplus is used, more and more programming modes are switched to client-side encoding, and parameters are often transmitted through URL conditions. Therefore, the querystring of a client is written to analyze the parameter values in a centralized manner.

 

 
 
FunctionQuerystring (paraname ){
VaRUrl = Window. Location. search;
VaRPara = URL. substr (URL. indexof (paraname ));
VaRAndindex = para. indexof ("&");
VaRParas;
If(Andindex>-1 ){
Paras = para. substr (0, para. indexof ("&"));
}Else{
Paras = para;
}
VaRParavalue = paras. Split ("=") [1];
If(Paravalue ){
ReturnDecodeuri (paravalue );
}Else{Return"";};
}

 

I used it all the time. Today, a colleague suddenly told me that during querystring, "the decoded URI is not legally encoded ". The problem is strange because the parameter has been correctly recognized by the client during the first entry, but this problem occurs after a button is triggered to execute PostBack. After checking, it is found that when the page is accessed for the first time, the ie url and form action address are different. After PostBack, the ie url is changed to the same action as from (refer to the demoCodeEncodeuri. aspx ). After testing, we found that this problem only exists in Chinese or special character encoding URLs. I found a lot of information on the Internet and did not get the correct answer (for example, setting requestencoding and responseencodeing. For Web. config). I did not get an effective solution after I had no communication with Alibaba Cloud. Later, I thought that since normal URLs won't go wrong, I would like to encode Chinese characters as non-special strings ......

Ii. Custom encoding and decoding methods: 

I have searched for many encoding and decoding methods, and I have never been able to find any suitable method. Therefore, I can only write such a function myself. Because both the client and server need to be called for encoding and decoding, it is not intended to be very complicated.

When searching for information, the problem of webcontrol dopostback parameter transfer from birdshome obtained the idea of separating the encoded strings by length, which is relatively simple and easy to write, therefore, we decided to use this idea for design.

Modify the function as follows:

Client:

 
 
FunctionQuerystring (paraname, iscusencode ){
VaRUrl = Window. Location. search;
VaRPara = URL. substr (URL. indexof (paraname ));
VaRAndindex = para. indexof ("&");
VaRParas;
If(Andindex>-1 ){
Paras = para. substr (0, para. indexof ("&"));
}Else{
Paras = para;
}
VaRParavalue = paras. Split ("=") [1];
If(Paravalue ){
ReturnKdecode (paravalue, iscusencode );
}Else{Return"";};
}
FunctionKencode (unzipstr, iscusencode ){
If(Iscusencode ){
VaRZiparray =NewArray ();
VaRZipstr ="";
VaRLens =NewArray ();
For(VaRI = 0; I <unzipstr. length; I ++ ){
VaRAC = unzipstr. charcodeat (I );
Zipstr + = ac;
Lens = Lens. Concat (AC. tostring (). Length );
}
Ziparray = ziparray. Concat (zipstr );
Ziparray = ziparray. Concat (lens. Join ("O"));
ReturnZiparray. Join ("N");
}Else{
ReturnEncodeuri (unzipstr );
}
}
FunctionKdecode (zipstr, iscusencode ){
If(Iscusencode ){
VaRZiparray = zipstr. Split ("N");
VaRZipsrcstr = ziparray [0];
VaRZiplens = ziparray [1]. Split ("O");
VaRUzipstr ="";
For(VaRJ = 0; j <ziplens. length; j ++ ){
VaRCharlen = parseint (ziplens [J]);
Uzipstr + = string. fromcharcode (zipsrcstr. substr (0, charlen ));
Zipsrcstr = zipsrcstr. Slice (charlen, zipsrcstr. Length );
}
ReturnUzipstr;
}Else{
ReturnDecodeuri (zipstr );
}
}

Of course, the server also needs the corresponding functions:

 

 
 
Public Static StringKencode (StringUnzipstr)
{
ReturnKencode (unzipstr,False);
}
Public Static StringKencode (StringUnzipstr,BoolIscusencode)
{
If(Iscusencode)
{
StringZipstr =String. Empty;
Stringcollection lens =NewStringcollection ();
Char[] Chars = unzipstr. tochararray ();
For(IntI = 0; I <chars. length; I ++)
{
IntAC = (Int) Chars [I];
Zipstr + = ac. tostring ();
Lens. Add (AC. tostring (). length. tostring ());
}
StringLen =String. Empty;
For(IntI = 0; I <lens. Count; I ++)
{
If(I + 1 = Lens. Count)
{
Len + = Lens [I];
}
Else
{
Len + = Lens [I] +"O";
}
}
ReturnZipstr +"N"+ Len;
}
Else
{
ReturnHttputility. urlencode (unzipstr );
}
}
Public Static StringKdecode (StringZipstr)
{
ReturnKdecode (zipstr,False);
}
Public Static StringKdecode (StringZipstr,BoolIscusencode)
{
If(Iscusencode)
{
String[] Ziparray = zipstr. Split (New String[1] {"N"}, Stringsplitoptions. removeemptyentries );
StringZipsourcestr = ziparray [0];
String[] Ziplens = ziparray [1]. Split (New String[1] {"O"}, Stringsplitoptions. removeemptyentries );// No Zero, is "O"
StringUzipstr ="";
For(IntJ = 0; j <ziplens. length; j ++)
{
IntCharlen =Int. Parse (ziplens [J]);
Uzipstr + = (Char)(Int. Parse (zipsourcestr. substring (0, charlen ))));
Zipsourcestr = zipsourcestr. Remove (0, charlen );
}
ReturnUzipstr;
}
Else
{
ReturnHttputility. urldecode (zipstr );
}
}

Iii. Post:

this is probably the worst solution. I wonder if anyone else in the garden has discovered this problem and has a better solution?

Problem description and solution DEMO code example : Download
updated: I was worried about the length limit of querystring. Later, I did not provide the relevant information: the maximum length of the IE Address Bar (-maximum URL length is 2,083 characters in Internet Explorer .), you can rest assured that the limit will not be exceeded.

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.