The request cannot obtain the correct Chinese parameters.

Source: Internet
Author: User
Tags urlencode
In ASP. NET, the request cannot obtain the correct Chinese parameter. For example, the Chinese value obtained from dropdownlist is incorrect. Why? How can this problem be solved ??

Server. urlencode (dropdownlist1.selecteditem. Text)

Add the following code to the Web. config file:

Http://schemas.microsoft.com/.NetConfiguration/v2.0>





Transfer page
Response. Redirect ("webform2.aspx? A = "+ server. urlencode (" w z c "));

Receiving page
String S = server. urldecode (request ["A"]. tostring ());

You can modify this line in Web. config to support Chinese characters.
Requestencoding = "gb2312"
Responseencoding = "gb2312"
/>

Encoding and decoding problems, if your users all use simplified Chinese,
Set the encoding/decoding character set to gb2312 in your web. config.
Requestencoding = "gb2312"
Responseencoding = "gb2312"

Header
Also changed

----

URL-based Chinese delivery solution
1. Set the Web. config file. (I don't like setting it like this)

......

......

2. Before passing Chinese characters, encode the Chinese parameters to be passed and decode them upon receiving them.
> Transfer
String name = "Chinese parameter ";
Response. Redirect ("B. aspx? Name = "+ server. urlencode (name ));
> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));

3. If a Chinese parameter is transferred from the. html file to the. aspx file (that is, URL conversion is not performed using the Redirect () method from the background ). Similarly, the passed Chinese parameters must be encoded and decoded upon receiving.
> Transfer

Function gourl ()
{
VaR name = "Chinese parameter ";
Location. href = "B. aspx? Name = "+ escape (name );
}

> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));

In general. Set the Web. config file. However, if you use JavaScript to call the WebService method (passing Chinese parameters to WebService ). The Web. config file is invalid.

 

In ASP. NET, the request cannot obtain the correct Chinese parameter. Solution: [base64 encoding/decoding]
The earliest reason for looking for this article is that the Chinese character after the parameter is added to the URL cannot be obtained in ASP. NET, so I want to change it to base64 encoding. However, it was still unsuccessful. The solution was found by the reminder of the bird food Xuan this morning. This is mainly because of the problem of using escape. The following two sections help to clearly explain the problem. I tried it. I don't know why the following code only displays two Chinese characters when the AA parameter is used to transmit three Chinese characters. It is a little strange that it can display all the four Chinese characters, if it is changed to ASP, it will be normal. However, one solution is to add encodeuri ("testurl. aspx? AA = test "); it is completely normal.

 

Void page_load (Object sender, eventargs evargs)
{
// Parameters passed when a new record is added
// If (! This. ispostback)
This. response. Write (request ["AA"]);

}

Function Test ()
{
Alert ("");
// Location = encodeuri ("testurl. aspx? AA = testing ");
Location = "testurl. aspx? AA = testing ";
}
// -->

Encodes string objects so they can be read on all computers.

Escape (charstring)
The required charstring argument is any string object or literal to be encoded.

Remarks
The escape method returns a string value (in unicode format) that contains the contents of charstring. all spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with % XX encoding, where XX is equivalent to the hexadecimal number representing the character. for example, a space is returned as "% 20."

Characters with a value greater than 255 are stored using the % uxxxx format.

Note the escape method shocould not be used to encode uniform resource identifiers (URI). Use encodeuri and encodeuricomponent methods instead.

Encodes a text string as a valid Uniform Resource Identifier (URI)

Encodeuri (uristring)
The required uristring argument is a value representing an encoded Uri.

Remarks
The encodeuri method returns an encoded Uri. if you pass the result to decodeuri, the original string is returned. the encodeuri method does not encode the following characters: ":", "/", ";", and "? ". Use encodeuricomponent to encode these characters.

Solved the problem of unsupported Chinese characters.
Encode the Chinese language with escape () and then use base64 encoding.
When decoding, add Unescape () to decode Chinese.

1
2
3
4
5
6 7
8 var keystr = "abcdefghijklmnop" +
9 "qrstuvwxyzabcdef" +
10 "ghijklmnopqrstuv" +
11 "wxyz0123456789 +/" +
12 "= ";
13
14 function encode64 (input ){
15 input = escape (input );
16 var output = "";
17 var chr1, chr2, chr3 = "";
18 var enc1, enc2, enc3, enc4 = "";
19 var I = 0;
20
21 do {
22 chr1 = input. charcodeat (I ++ );
23 chr2 = input. charcodeat (I ++ );
24 chr3 = input. charcodeat (I ++ );
25
26 enc1 = chr1> 2;
27 enc2 = (chr1 & 3)> 4 );
28 enc3 = (chr2 & 15)> 6 );
29 enc4 = chr3 & 63;
30
31 if (isnan (chr2 )){
32 enc3 = enc4 = 64;
33} else if (isnan (chr3 )){
34 enc4 = 64;
35}
36
37 output = output +
38 keystr. charat (enc1) +
39 keystr. charat (enc2) +
40 keystr. charat (enc3) +
41 keystr. charat (enc4 );
42 chr1 = chr2 = chr3 = "";
43 enc1 = enc2 = enc3 = enc4 = "";
44} while (I 45
46 Return output;
47}
48
49 function decode64 (input ){
50 var output = "";
51 var chr1, chr2, chr3 = "";
52 var enc1, enc2, enc3, enc4 = "";
53 var I = 0;
54
55 // remove all characters that are not A-Z, A-Z, 0-9, +,/, OR =
56 var base64test =/[^ A-Za-z0-9/+ // =]/g;
57 if (base64test.exe C (input )){
58 alert ("there were invalid base64 characters in the input text./N" +
59 "valid base64 characters are A-Z, A-Z, 0-9, '+', '/', and '='/N" +
60 "reverse CT errors in decoding .");
61}
62 input = input. Replace (/[^ A-Za-z0-9/+ // =]/g ,"");
63
64 do {
65 enc1 = keystr. indexof (input. charat (I ++ ));
66 enc2 = keystr. indexof (input. charat (I ++ ));
67 enc3 = keystr. indexof (input. charat (I ++ ));
68 enc4 = keystr. indexof (input. charat (I ++ ));
69
70 chr1 = (enc1> 4 );
71 chr2 = (enc2 & 15)> 2 );
72 chr3 = (enc3 & 3) 73
74 output = output + String. fromcharcode (chr1 );
75
76 if (enc3! = 64 ){
77 output = output + String. fromcharcode (chr2 );
78}
79 if (enc4! = 64 ){
80 output = output + String. fromcharcode (chr3 );
81}
82
83 chr1 = chr2 = chr3 = "";
84 enc1 = enc2 = enc3 = enc4 = "";
85
86} while (I 87
88 return Unescape (output );
89}
90
91 // -->
92
93
94
95
96
97 type in the message you want to encode in base64, or paste

98 base64 encoded text into the text field, select encode or decode,

99 and click the button!

100
101

102
103 104 onclick = "document. base64form. thetext. value = encode64 (document. base64form. thetext. Value);">
105 106 onclick = "document. base64form. thetext. value = decode64 (document. base64form. thetext. Value);">
107
108
109
110
111
112

Http://liubiqu.cnblogs.com/archive/2005/05/24/161506.html

 

 

Summary of solutions for transmitting Chinese Characters

From: csdn culturenet
1. Set the Web. config file.
<System. Web>
......
<Globalization requestencoding = "gb2312" responseencoding = "gb2312" Culture = "ZH-CN" fileencoding = "gb2312"/>
......
</System. Web>
 
Or:
In the aspx file:
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

2. Before passing Chinese characters, encode the Chinese parameters to be passed and decode them upon receiving them.
> Transfer
String name = "Chinese parameter ";
Response. Redirect ("B. aspx? Name = "+ server. urlencode (name ));

> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));
Or:
 
Navigateurl = '<% # "webform2.aspx? Singer = "+ httputility. urlencode (" Chinese ", system. Text. encoding. getencoding (" gb2312 ") %>'

3. If a Chinese parameter is transferred from the. html file to the. aspx file (that is, URL conversion is not performed using the Redirect () method from the background ). Similarly, the passed Chinese parameters must be encoded and decoded upon receiving.
> Transfer
<Script language = "JavaScript">
Function gourl ()
{
VaR name = "Chinese parameter ";
Location. href = "B. aspx? Name = "+ escape (name );
}
</SCRIPT>
<Body onclick = "gourl ()">
> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));

In general. Set the Web. config file. However, if you use JavaScript to call the WebService method (passing Chinese parameters to WebService ). The Web. config file is invalid.

--------------------
Codec in HTML:

<Script language = "JavaScript">
Function Openurl (SRC)
{
VaR strurl = escape (SRC );
Window. Open (strurl );
}
 

Function change_url (SRC)
{
Document. Location. href = escape (SRC );
}
 
</SCRIPT>
 
Save in new window
<A href = 'blocked scriptopenurl ("CSS/ .doc"); '> submit the contract </a>
 
 
The current location is saved without blinking.
<A href = "#" onclick = blocked scriptchange_url ("CSS/Contract .doc")> contract </a>
 
Note: The slash in the path is "/" instead of "/". Otherwise, it will not work.
 
 
 
 
----------------------------------------
When I was developing a. NET web, I encountered a very strange problem, that is, in the URL, if the Chinese character is passed as the parameter value, the value of querystring may be wrong. To put it simply, for example, the following URL:
Urlparmtest. aspx? Parm1 = China & parm2 = Chinese
In request. querystring, parm1 and parm2 get both "China". Obviously there is a problem, but in some cases it is normal.

If the request uses response. Redirect instead of a URL, no similar problems have been encountered.

At that time, I thought that the Chinese language was double-byte encoding. It was possible that there was uncertainty when it was passed, but it was still good to use English.

But why is redirect on the server normal? Where is the problem?

Yesterday I asked my colleagues at gtec about the following:
If you set Chinese parameters in the. CS file, encode the Chinese parameters using server. urlencode ("Chinese ").
If you set it in the. aspx file, use <% = server. urlencode ("Chinese") %> to encode
When querystring is used, decode is not required to obtain a normal Chinese string.

The following are some explanations:
Urlencode converts several multi-byte characters into the single-byte characters allowed in the URL, which will be automatically implemented by the browser. However, there are some problems, so encode it yourself, the receiver automatically performs decode on the URL.

I think response. Redirect may be able to ensure the work of The encode, so there is no problem.

Add By: huobazi

 

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.