I made my own website and found that Baidu's spider and Google's spider had different URL encoding and decoding methods, which caused me a lot of confusion,
A function is implemented to unify the decoding and the world is quiet.
By the way, I would like to publicize the website address
Protected void page_load (Object sender, eventargs E)
{
Response. Write (urldecode ("XXX "));
}
/// <Summary>
/// General URL Decoding
/// First try UTF-8 and then gb2312
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string UrlDecode (string key)
{
String input = GetUrlParam (key). ToLower ();
If (input. Length = 0)
Return string. Empty;
// First, use UTF-8 for decoding.
String result = HttpUtility. UrlDecode (input, Encoding. UTF8 );
// Encode the decoded characters again.
String encode = HttpUtility. UrlEncode (result, Encoding. UTF8). ToLower ();
// Compare with the original encoding. If the encoding is inconsistent, the decoding is incorrect. Use gb2312 for decoding.
If (input! = Encode)
Result = HttpUtility. UrlDecode (input, Encoding. GetEncoding ("gb2312 "));
Return result;
}
/// <Summary>
/// Obtain the Url parameters, without encoding. Only the plaintext is obtained.
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Private static string GetUrlParam (string key)
{
If (System. Web. HttpContext. Current! = Null)
{
String query = HttpContext. Current. Request. Url. Query;
If (query! = Null & query. Length> 0)
{
Int index = 0;
Index = query. IndexOf (key + "= ");
If (index> = 0)
{
Query = query. Substring (key. Length + 1 + index );
Index = query. IndexOf ('&');
If (index> = 0)
Query = query. Substring (0, index );
Return query;
}
}
}
Return string. Empty;
}