在ASP.NET中Request取不到正确的中文参数问题解决办法[base64编码/解码]

最早想找这篇文章的原因是参数加在URL后的中文在ASP.NET里没有办法取到,所以想改成Base64编码试一下。但还是没有成功,今天早上经鸟食轩的提醒倒是找到了解决方法。主要是用escape的问题,下面有两段帮助里面写的比较清楚,我试了一下确实可以了。不知道为什么下面的这段代码当参数aa传三个中文汉字的时候就只能显示两个,传4个汉字的时候倒可以全部显示,真是有点奇怪,如果换成ASP就很正常了。不过有一个解决办法就是加了encodeURI("testUrl.aspx?aa=测试内");就完全正常了。

<% @ Page Language="C#" EnableSessionState="true"  %>
<% @ Import Namespace="System" %>
<% @ Import Namespace="System.Web" %>
< script  language ="C#"  runat =server >

void Page_Load(Object sender, EventArgs EvArgs) 
{
    
//新增记录时传递的参数
    //if(!this.IsPostBack)
    this.Response.Write(Request["aa"]);

}
   
</ script >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< TITLE >  New Document  </ TITLE >
< META  NAME ="Generator"  CONTENT ="EditPlus" >
< META  NAME ="Author"  CONTENT ="" >
< META  NAME ="Keywords"  CONTENT ="" >
< META  NAME ="Description"  CONTENT ="" >
< SCRIPT  LANGUAGE ="JavaScript" >
<!--
function test()
{
    alert(
"<% =Request["aa"]%>");
    
//location=encodeURI("testUrl.aspx?aa=测试内");
    location="testUrl.aspx?aa=测试内";
}

//-->
</ SCRIPT ></ HEAD >

< BODY >
< INPUT  TYPE ="button"  value =test  onclick ="test();" >

</ BODY >
</ HTML >


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 should 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.


解决了对中文的不支持问题.
先用escape()对中文进行编码.然后再进行base64编码.
解码时,再加入unescape()对中文进行解码.

  1 < html >
  2     < head >
  3        < title > base64 Encoding/Decoding </ title >
  4     </ head >
  5
  6     < script  type ="text/javascript" > <!--
  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| (chr2 >> 4);
 28         enc3 = ((chr2 & 15<< 2| (chr3 >> 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 < input.length);
 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.exec(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               "Expect 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 << 2| (enc2 >> 4);
 71         chr2 = ((enc2 & 15<< 4| (enc3 >> 2);
 72         chr3 = ((enc3 & 3<< 6| enc4;
 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 < input.length);
 87
 88      return unescape(output);
 89   }

 90
 91   //--></script>
 92
 93   <body>
 94
 95      <form name="base64Form">
 96
 97         Type in the message you want to encode in base64, or paste<br>
 98         base64 encoded text into the text field, select Encode or Decode, <br>
 99         and click the button!<br>
100
101         <textarea name="theText" cols="40" rows="6"></textarea><br>
102
103         <input type="button" name="encode" value="Encode to base64"
104            onClick="document.base64Form.theText.value=encode64(document.base64Form.theText.value);">
105         <input type="button" name="decode" value="Decode from base64" 
106            onClick="document.base64Form.theText.value=decode64(document.base64Form.theText.value);">
107
108      </form>
109
110   </body>
111</html>
112
113
114
115

你可能感兴趣的:(asp.net)