C # Chinese URL Encoding

Source: Internet
Author: User

StudyingProgramEnter a keyword to send the keyword to search engines such as Google and Yahoo, and then open the result webpage. The principle is simple. For example
Google search for China, the search results page url is "http://www.google.com/search? Hl = ZH-CN &
Q = China & LR = ". You only need to replace the red content to search by different keywords.

However, if the keyword is Chinese, the problem may occur. For example, when Google searches for "China", the URL is "http://www.google.com"
/Search? Hl = ZH-CN & newwindow = 1 & Q = % E4 % B8 % ad % E5 % 9B % BD & LR = ". Chinese character "medium"
Country is encoded according to the format of UTF-8.

Not only Chinese characters are encoded, but some special characters are also encoded. For example, to search for "C #", the URL is "http://www.google.com/search? Hl = ZH-CN & newwindow = 1 & Q = C % 23 & LR = ".

In general, foreign websites are according to the UTF-8 code, and "Baidu" is according to "gb2312" encoding. For example, if you search for "China", the URL is "http://www.baidu.com/s? WD = % D6 % D0 % B9 % fa & CL = 3"

Let's compare: C # China Code

Encoding result website
UTF-8 C % 23% E4 % B8 % ad % E5 % 9B % BD Google
Gb2312 C % 23% D6 % D0 % B9 % fa Baidu

Summary:
In the UTF-8, a Chinese character pair should be three bytes, and a Chinese character in gb2312 occupies two bytes.
No matter what encoding, letters and numbers are not encoded, and the special symbol encoding occupies one byte.

// Code by UTF-8
String tempsearchstring1 = system. Web. httputility. urlencode ("C # China ");
// Encoding by gb2312
String tempsearchstring2 = system. Web. httputility. urlencode ("C # China", system. Text. encoding. getencoding ("gb2312 "));

// Configure //--------------------------------------------------------------------------------------------------------------

[Switch] process the URL encoding of C # in ASP. NET
Problems to be Solved:
Upload the following URL as a parameter to other pages
1 http: // domain/DE. retrial? Uid = 12 & page = 15
2. The parameters following the URL contain Chinese characters, such as:... aspx? Title = crane
In the above case, an rul encoding and decoding process must be performed; otherwise, an error may occur.

CodeAs follows:

// Pass the value
String temp = "<a href = 'add. aspx? Url = "+ server. urlencode (
Skin. Page. Request. url. absoluteuri) + "& Title =" + server. urlencode (
Skin. Page. header. Title) + "'> Add to favorites </a> ");

// Obtain the value from the above in another file
If (request. querystring ["url"]! = NULL)
{
String url = server. urldecode (request. querystring ["url"]. tostring ());
This.txt address. Text = URL;
}
If (request. querystring ["title"]! = NULL)
{
String title = server. urldecode (request. querystring ["title"]. tostring ());
This.txt title. Text = title;
}

//-----------------------------------------------

URL encoding table

1. String S = system. Web. httputility (byte [] data );

Here, S is the converted URL encoding. Note that the byte array must be an ASCII array. text. encoding. default. getbytes (Str. tochararray (); is incorrect and cannot be escaped correctly!

2. Write a small program based on URL encoding rules

* ***** String urlencode (byte [] BYT)
{
String desstr = "";
For (INT I = 0; I <BYT. length; I ++)
{
Desstr + = "% ";
Desstr + = BYT [I]. tostring ("X2 ");
}
Return desstr;
}

The URL encoding table is as follows:

Backspace % 08
Tab % 09
Linefeed % 0a
Creturn % 0d
Space % 20
! % 21
"% 22
# % 23
$ % 24
% 25
& % 26
'% 27
(% 28
) % 29
* % 2a
+ % 2B
, % 2C
-% 2D
. % 2e
/% 2f
0% 30
1% 31
2% 32
3% 33
4% 34
5% 35
6% 36
7% 37
8% 38
9% 39
: % 3A
; % 3B
<% 3C
= % 3d
> % 3E
? % 3f
@ % 40
A % 41
B % 42
C % 43
D % 44
E % 45
F % 46
G % 47
H % 48
I % 49
J % 4A
K % 4B
L % 4C
M % 4D
N % 4E
O % 4f
P % 50
Q % 51
R % 52
S % 53
T % 54
U % 55
V % 56
W % 57
X % 58
Y % 59
Z % 5A
[% 5b
\ % 5c
] % 5D
^ % 5E
_ % 5f
'% 60
A % 61
B % 62
C % 63
D % 64
E % 65
F % 66
G % 67
H % 68
I % 69
J % 6a
K % 6b
L % 6c
M % 6d
N % 6e
O % 6f
P % 70
Q % 71
R % 72
S % 73
T % 74
U % 75
V % 76
W % 77
X % 78
Y % 79
Z % 7A
{% 7b
| % 7c
} % 7D
~ % 7E
Snapshot % A2
Certificate % A3
¥ % A5
| % A6
§ % A7
? % AB
? % AC
Memory % ad
O % B0
± % B1
A % B2
, % B4
μ% B5
? % Bb
? % BC
? % BD
? % BF
A' % C0
A' % C1
A ^ % C2
A ~ % C3
A' % c4
A ° % C5
? % C6
C? % C7
E' % C8
E '% C9
E ^ % Ca
E? % CB
I '% CC
I '% Cd
I ^ % Ce
¨ % Cf
D % D0
N ~ % D1
O' % D2
O '% D3
O ^ % D4
O ~ % D5
O? % D6
? % D8
U' % D9
U' % da
U ^ % db
U? % DC
Y' % dd
T % de
? % DF
A' % E0
A' % E1
A ^ % E2
A ~ % E3
A' % E4
A ° % E5
? % E6
C? % E7
E' % E8
E' % E9
E ^ % EA
E? % EB
I '% EC
I '% ed
I ^ % EE
I evaluate % ef
E % F0
N ~ % F1
O' % F2
O' % F3
O ^ % F4
O ~ % F5
O? % F6
Failed % F7
? % F8
U' % F9
U' % fa
U ^ % FB
U evaluate % FC
Y' % FD
T % Fe
Y' % FF

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.