日期:2014-05-17  浏览次数:20406 次

如何获取.net 地址栏中的中文参数值?
我现在有一个地址为:http://www.××.com/index.aspx?name=天堂
我该怎么在.net页面获得这个参数?
因为我在页面中获得的是繁体字或者是乱码,我不知道该怎么解决,
帮帮忙
我这个‘天堂’应该是utf-8的编码,而我的.net页面是gb2312的


------解决方案--------------------
你可以用加密和解密来进行获取URL的中文参数;
------解决方案--------------------
加密 解密
------解决方案--------------------
/// <summary>
/// 加密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}

/// <summary>
/// Des 解密 GB2312 
/// </summary>
/// <param name="str">Desc string</param>
/// <param name="key">Key ,必须为8位 </param>
/// <returns></returns>
public static string Decode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}

调用方法
/// <summary>
/// If don't input key , Use default key
/// Des 加密 GB2312 :
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encode(string str)
{
return VerifyTool.Encode(str, "Rainight");
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decode(string str)
{
return VerifyTool.Decode(str, "Rainight");
}
------解决方案--------------------
首先是,添加引用,添加对于System.Web的引用。可能要引入两次,一次是导入System.Web.dll,一次是,在程序集上面加入命名空间的引用。 

接着,请参考下面的代码: 

1 string str1=textBox1.Text;