无法在网址中编码查询字符串. [英] Unable to encode a querystring in url.

查看:65
本文介绍了无法在网址中编码查询字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Response.Redirect("WebForm2.Aspx?Parameter=" + Server.UrlEncode(txtName.Text));


但是它没有编码.


But its not encoding.

推荐答案

您做错了.

我知道,我的答案似乎模糊不清,甚至有些晦涩,但问题的高低决定了答案的质量.

提示:您要编码的字符串的内容是什么?
You''re doing it wrong.

I know, my answer might seem vague, or even obtuse, but the qualify of the question dictates the quality of the answer.

Hint: WHAT is the content of the string you''re trying to encode?


URLEncode将空格()转换为加号(+),并将非字母数字字符转换为十六进制表示形式.它不会转换字符串.

您可以使用以下编码机制将其转换为其他格式.
URLEncode converts Spaces ( ) to plus signs (+) and Non-alphanumeric characters to their hexadecimal representation. It will not convert string.

You can use the below encoding mechanism to convert to different format.
public string fnEncode(string str)
{
	byte[] byteEncode = new byte[str.Length + 1];
	string strEncoded = null;

	byteEncode = System.Text.Encoding.UTF8.GetBytes(str);
	strEncoded = Convert.ToBase64String(byteEncode);

	return strEncoded;
}


并称呼它.


and call this.

Response.Redirect("WebForm2.Aspx?Parameter=" + fnEncode(txtName.Text));



用于解码:



For Decode:

public string fnBase64Decode(string strPwd)
{
	System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
	System.Text.Decoder utf8Decode = null;
	byte[] byteDecode = null;
	int intCharCnt = 0;
	string strDecoded = null;

	utf8Decode = encoder.GetDecoder();
	byteDecode = Convert.FromBase64String(strPwd);
	intCharCnt = utf8Decode.GetCharCount(byteDecode, 0, byteDecode.Length);

	char[] charDecoded = new char[intCharCnt + 1];

	utf8Decode.GetChars(byteDecode, 0, byteDecode.Length, charDecoded, 0);
	strDecoded = new string(charDecoded);

	return strDecoded;
}


使用此
public static string Encrypt(string toEncrypt, string key, bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return Convert.ToBase64String(resultArray, 0, resultArray.Length).Replace("+"," ");
    }


    public static string Decrypt(string toDecrypt, string key, bool useHashing)
    {
        toDecrypt = toDecrypt.Replace(" ", "+");
        byte[] keyArray;
        byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return UTF8Encoding.UTF8.GetString(resultArray);
    }



调用时使用语法



While calling use the syntax

string Id = Decrypt(ID.ToString().Replace("


这篇关于无法在网址中编码查询字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆