PDA

View Full Version : کاراکتر + توی کوئری استرینگ



Himalaya
چهارشنبه 18 آذر 1388, 10:03 صبح
publicstring Encode(string MyString)
{
string result;
try
{
byte[] IV = newbyte[8] { 240, 32, 45, 29, 0, 76, 173, 59 };
string cryptoKey = "Agg ois sdda bb Lopb oao nffex";
byte[] buffer = Encoding.ASCII.GetBytes(MyString);
TripleDESCryptoServiceProvider des = newTripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MD5 = newMD5CryptoServiceProvider();
des.Key = MD5.ComputeHash(Encoding.ASCII.GetBytes(cryptoKey) );
des.IV = IV;
byte[] CodedBuffer = des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length);
result = Convert.ToBase64String(CodedBuffer, 0, CodedBuffer.Length);
}
catch
{
result = null;
}
return result;
}

////////////////////////////////////////////////////////////////////////////

publicstring Decode(string CodedString64)
{
string result;
try
{
byte[] IV = newbyte[8] { 240, 32, 45, 29, 0, 76, 173, 59 };
string cryptoKey = "Agg ois sdda bb Lopb oao nffex";
byte[] buffer = Convert.FromBase64String(CodedString64);
TripleDESCryptoServiceProvider des = newTripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MD5 = newMD5CryptoServiceProvider();
des.Key = MD5.ComputeHash(Encoding.ASCII.GetBytes(cryptoKey) );
des.IV = IV;
byte[] CodedBuffer = des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length);
result = Encoding.ASCII.GetString(CodedBuffer);
}
catch
{
result = "";
}
return result;
}


یادم رفت اول سلام:لبخند:
اما مشکلم... میخوام بدونم کاراکتر + توی کوئری استرینگ کار خاصی انجام میده یا نه...
آخه من با 2 تابعی که تو اینجا گفتم کوئری استرینگ رو کد میکنم و پاس میدم به صفحه بعدی
... حالا اگه مثلا کاراکتر 1 رو کد کنم میشه مثلا این HWDldgUdKYg= که وقتی تو یه لیبیل این مقدار رو میگیرم و نمایش میدم همینو نشون میده ولی وقتی که مثلا 2 رو کد میکنم کوئری استرینگم میشه این VwuGJ1+W09k= که وقتی اونو میخونم و تو یه لیبل نمایش میدم میشه این VwuGJ1 W09k= ...یعنی به جای + اسپیس قرار میده که این باعث میشه دیگه نتونم به مقدار اولیه دسترسی پیدا کنم... این 2 تا تابع رو هم از همین سایت گرفتم
وقتی مقادیر رو توی سشن میریزم ،همه چیز درسته، اما نمیخوام با سشن کار کنم... تو کوکی هم همین مشکل وجود داره
حالا 2 تا سوال
1. میتونم مثلا بگم که اگه توی کوئری استرینگ اسپیس بود اونو با تابع ریپلیس تبدیل کن به + ولی خوب ممکنه که اون کاراکتر واقعا اسپیس باشه که در این صورت تبدیل کردنش اشتباه هستش... تا نظر دوستان چی باشه (البته این بیشتر نظر سنجی بود تا سوال :لبخند:)
2. آیا این مورد فقط واسه کاراکتر + پیش میاد یا ممکنه کاراکترهای دیگه ای هم این حالتو پیش بیارن
یه راه حل میخوام واسه این مشکل، لطفا
ممنون

Chabok
چهارشنبه 18 آذر 1388, 19:18 عصر
در URL بعضی کارکترها مانند Space نامعتبر هستند
از تابع Server.UrlEncode استفاده کنید تا کارکترهای غیر مجاز در URL به مقادیر جایگزین تبدیل شوند .


The following script:
<%Response.Write(Server.URLEncode("http://www.microsoft.com")) %>
produces the following output:
http%3A%2F%2Fwww%2Emicrosoft%2Ecomتوضیحات بیشتر :


URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. As a result, these characters must be encoded in <a> tags or in query strings where the strings can be re-sent by a browser in a request string.
UrlEncode (http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urlencode.aspx) is a convenient way to access the HttpUtility..::.UrlEncode (http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx) method at run time from an ASP.NET application. Internally, UrlEncode (http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urlencode.aspx) uses HttpUtility..::.UrlEncode (http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx) to encode strings.
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

Himalaya
چهارشنبه 18 آذر 1388, 21:35 عصر
مشکل من سر کاراکتر تو آدرس بار نیست که...


مثلا 2 رو کد میکنم کوئری استرینگم میشه این VwuGJ1+W09k=

که این دقیقا همون چیزیه که باید باشه
مشکل من اینه که اون + تو کوئری استرینگ هست ولی وقتی کوئری استرینگ رو میخونم و میریزم تو لیبل به اسپیس تبدیل میسه
در حالی که تو سشن که میریزم و از سشن میخونم همه چیز درسته
توجه کنید که مقداری که تو کوئری استرینگ قرار میگیره با چیزی که تو سشن قرار میگیره دقیقا یکی هست ولی اختلاف این دو زمانی مشخص میشه که میخوام مقادیر رو از اونا بخونم...

مهدی کرامتی
چهارشنبه 18 آذر 1388, 22:59 عصر
نقل از ویکی پدیا:
URL encoding

Main article: URL encoding (http://en.wikipedia.org/wiki/URL_encoding)
Some characters (http://en.wikipedia.org/wiki/Character_%28computing%29) cannot be part of a URL (for example, the space) and some other characters have a special meaning in a URL: for example, the character # can be used to further specify a subsection (or fragment (http://en.wikipedia.org/wiki/Fragment_identifier)) of a document; the character = is used to separate a name from a value. A query string may need to be converted to satisfy these constraints. This can be done using a schema known as URL encoding (http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters).
In particular, encoding the query string uses the following rules:


Letters (A-Z and a-z), numbers (0-9) and the characters '.', '-', '~' and '_' are left as-is
SPACE is encoded as '+'
All other characters are encoded as %FF hex (http://en.wikipedia.org/wiki/Hexadecimal) representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)

The encoding of SPACE as '+' and the selection of "as-is" characters distinguishes this encoding from RFC 1738 (http://tools.ietf.org/html/rfc1738).

لینک مرجع: http://en.wikipedia.org/wiki/Query_string

Himalaya
پنج شنبه 19 آذر 1388, 01:03 صبح
مرسی از داداشای گلم