asp.net querystring mess code solution

  • 2020-11-18 06:09:26
  • OfStack

Under normal circumstances, many asp.net sites now use UTF8 directly for page coding, which is the same as Javascript's default site code, but quite a few use GB2312

For GB2312 website if direct use javascript ajax data submission, for example: http: / / www xxx. com/accept aspx? name= 3, or UTF8's website with the following asp.net code to submit, is also not acceptable, will cause querystring confusion.


WebRequest request = WebRequest.Create("http://www.xxx.com/accept.aspx?name= zhang 3");  
request.Method = "POST";  
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Request.QueryString ["name"] is a mess. MS has encapsulated the code conversion block.

There are many ways of encoding conversion in UTF8 and GB2312 communication:

The first type: UrlEncode is applied to the character to be transmitted first, and the encoded character is manually decoded with UTF8 encoding method during decoding, so as to ensure the result is 1. Even when the target page is transmitted to GB2312, the result is 1, avoiding querystring confusion. The decoding method is as follows.


HttpUtility.UrlDecode(s, Encoding.UTF8); 

In this way, you can get the correct 3, which requires you to encode ES47en.ES48en into UTF8 before putting it into name=(the character after encoding). This is also a common and common solution at present, but one disadvantage is that you need to tell others how to encode Url first, and then how to do it.

2 kinds: comparison of alternative 1, the client to submit the byte directly read data transformation, the Request. QueryString [" name "] will be garbled, MS is transformed according to the current page code, such as the current page is coding GB2312, and somebody else is UTF8 submitted, you do not use somebody else submit UTF8 coding is, of course, the code, and not somebody else is garbled. At this time, we need to get the original data and redecode it to avoid the messy code of querystring. Unfortunately, I did not find the method to directly provide the original byte data of the head for us. It doesn't matter.


public NameValueCollection QueryString {  
          get {   
              if (_queryString == null) {  
                  _queryString = new HttpValueCollection();   

                  if (_wr != null)  
                      FillInQueryStringCollection();   

                  _queryString.MakeReadOnly();  
              }  

              if (_flags[needToValidateQueryString]) {  
                  _flags.Clear(needToValidateQueryString);   
                  ValidateNameValueCollection(_queryString, "Request.QueryString");   
              }  

              return _queryString;  
          }  
      }  


private void FillInQueryStringCollection()  
{  
    byte[] queryStringBytes = this.QueryStringBytes;  
    if (queryStringBytes != null)  
    {  
        if (queryStringBytes.Length != 0)  
        {  
            this._queryString.FillFromEncodedBytes(queryStringBytes, this.QueryStringEncoding);  
        }  
    }  
    else if (!string.IsNullOrEmpty(this.QueryStringText))  
    {  
        this._queryString.FillFromString(this.QueryStringText, true, this.QueryStringEncoding);  
    }  
}  

By the way, QueryString was initialized the first time it was accessed, and if you don't use it in your program, that object will remain empty until 1. MS takes care of the details

You see the QueryStringBytes attribute, the prototype is internal byte[] QueryStringBytes, and this is the original QueryString byte. Pushing the:


Type type = Request.GetType();  
PropertyInfo property = type.GetProperty("QueryStringBytes",  
BindingFlags.Instance  | BindingFlags.IgnoreCase | BindingFlags.NonPublic);  
byte[] queryBytes = (byte[])property.GetValue(Request, null);  
string querystring = HttpUtility.UrlDecode(queryBytes, Encoding.UTF8);  

Now let's see what querystring is, haha name= 3.

All kinds of code conversion can be done by yourself, after all, the submitted original byte, I hope to help you solve the querystring mess.


Related articles: