关于ASP.NET中Request.QueryString的乱码问题
时间:2010-10-18 来源:情缘鸟
'Visual Basic.NET
Dim stringValue As String
stringValue = Request.QueryString("string")
Response.Write(stringValue)
//Visual C#
string stringValue;
stringValue = Request.QueryString["string"];
Response.Write(stringValue);
'引用System.Collections.Specialized和System.Text命名空间
Dim stringValue As String
Dim gb2312Requests As NameValueCollection
gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312"))
Response.Write(gb2312Requests("string")) '里面的string就是你提交的参数的Key
//Visual C#
'引用System.Collections.Specialized和System.Text命名空间
string stringValue;
NameValueCollection gb2312Requests;
gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312"))
Response.Write(gb2312Requests["string"]); //'里面的string就是你提交的参数的Key
Dim strBefore As String = "风の世界"
Dim strAfter As String = ""
strAfter = Server.UrlEncode(strBefore)
Response.Write(strAlfter)
//Visual C#
string strBefore = "风の世界";
string strAlfter = "";
strAfter = Server.UrlEncode(strBefore);
Response.Write(strAlfter);
'引用System.Text命名空间
Dim strBefore As String = "风の世界"
Dim strAfter As String = ""
strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312"))
Response.Write(strAlfter)
//Visual C#
//引用System.Text命名空间
string strBefore = "风の世界";
string strAlfter = "";
strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312"));
Response.Write(strAlfter);
Dim stringValue As String
stringValue = Request.QueryString("string")
Response.Write(stringValue)
//Visual C#
string stringValue;
stringValue = Request.QueryString["string"];
Response.Write(stringValue);
2、提交的参数是经过GB2312 URL编码的。
在这种情况下,就不能直接取值了。可以用下面的方法:
'引用System.Collections.Specialized和System.Text命名空间
Dim stringValue As String
Dim gb2312Requests As NameValueCollection
gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312"))
Response.Write(gb2312Requests("string")) '里面的string就是你提交的参数的Key
//Visual C#
'引用System.Collections.Specialized和System.Text命名空间
string stringValue;
NameValueCollection gb2312Requests;
gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312"))
Response.Write(gb2312Requests["string"]); //'里面的string就是你提交的参数的Key
有的时候,我们还想提交不同编码的URL编码字符串,可以接着往下看。
3、提交UTF8 URL编码参数。
前面已经说过,在不指定编码的时候,系统是自动使用UTF-8编码的,那么我们要提交UTF8 URL编码参数可以直接使用Server.UrlEncode。代码如下:
Dim strBefore As String = "风の世界"
Dim strAfter As String = ""
strAfter = Server.UrlEncode(strBefore)
Response.Write(strAlfter)
//Visual C#
string strBefore = "风の世界";
string strAlfter = "";
strAfter = Server.UrlEncode(strBefore);
Response.Write(strAlfter);
4、提交GB2312 URL编码参数。
因为系统默认使用的是UTF-8编码,所以要用GB2312进行URL编码。得指定一个编码才行。代码如下:
'引用System.Text命名空间
Dim strBefore As String = "风の世界"
Dim strAfter As String = ""
strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312"))
Response.Write(strAlfter)
//Visual C#
//引用System.Text命名空间
string strBefore = "风の世界";
string strAlfter = "";
strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312"));
Response.Write(strAlfter);
这样,URL编码后得到的就是GB2312的编码字符了。
另外要注意的地方就是,ASP中Server.UrlEncode是以GB2312编码进行URL编码的。
相关阅读 更多 +