PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/App_Code/modules/Westwind.IternetTools/wwHttpUtils.cs

http://pigeoncms.googlecode.com/
C# | 102 lines | 67 code | 14 blank | 21 comment | 22 complexity | 1338489afc094ee5fe9689cc33cad808 MD5 | raw file
  1using System;
  2using System.Text;
  3using System.IO;
  4
  5namespace Westwind.InternetTools
  6{
  7	/// <summary>
  8	/// wwHttp Utility class to provide UrlEncoding without the need to use
  9	/// the System.Web libraries (too much overhead)
 10	/// </summary>
 11	public class wwHttpUtils 
 12	{
 13		/// <summary>
 14		/// UrlEncodes a string without the requirement for System.Web
 15		/// </summary>
 16		/// <param name="String"></param>
 17		/// <returns></returns>
 18		public static string UrlEncode(string InputString) 
 19		{
 20			StringReader sr = new StringReader( InputString);
 21			StringBuilder sb = new StringBuilder(  InputString.Length );
 22
 23			while (true) 
 24			{
 25				int lnVal = sr.Read();
 26				if (lnVal == -1)
 27					break;
 28				char lcChar = (char) lnVal;
 29
 30				if (lcChar >= 'a' && lcChar < 'z' || 
 31					lcChar >= 'A' && lcChar < 'Z' || 
 32					lcChar >= '0' && lcChar < '9')
 33					sb.Append(lcChar);
 34				else if (lcChar == ' ') 
 35					sb.Append("+");
 36				else
 37					sb.AppendFormat("%{0:X2}",lnVal);
 38			}
 39
 40			return sb.ToString();
 41		}
 42
 43		/// <summary>
 44		/// UrlDecodes a string without requiring System.Web
 45		/// </summary>
 46		/// <param name="InputString">String to decode.</param>
 47		/// <returns>decoded string</returns>
 48		public static string UrlDecode(string InputString)
 49		{
 50			char temp = ' ';
 51			StringReader sr = new StringReader(InputString);
 52			StringBuilder sb = new StringBuilder( InputString.Length );
 53
 54			while (true) 
 55			{
 56				int lnVal = sr.Read();
 57				if (lnVal == -1)
 58					break;
 59				char TChar = (char) lnVal;
 60				if (TChar == '+')
 61					sb.Append(' ');
 62				else if(TChar == '%') 
 63				{
 64					// *** read the next 2 chars and parse into a char
 65					temp = (char) Int32.Parse(((char) sr.Read()).ToString() +  ((char) sr.Read()).ToString(),
 66												   System.Globalization.NumberStyles.HexNumber);
 67					sb.Append(temp);
 68				}
 69				else
 70					sb.Append(TChar);
 71			}
 72
 73			return sb.ToString();
 74		}
 75
 76		/// <summary>
 77		/// Retrieves a value by key from a UrlEncoded string.
 78		/// </summary>
 79		/// <param name="UrlEncodedString">UrlEncoded String</param>
 80		/// <param name="Key">Key to retrieve value for</param>
 81		/// <returns>returns the value or "" if the key is not found or the value is blank</returns>
 82		public static string GetUrlEncodedKey(string UrlEncodedString, string Key) 
 83		{
 84			UrlEncodedString = "&" + UrlEncodedString + "&";
 85
 86			int Index = UrlEncodedString.ToLower().IndexOf("&" + Key.ToLower() + "=");
 87			if (Index < 0)
 88				return "";
 89	
 90			int lnStart = Index + 2 + Key.Length;
 91
 92			int Index2 = UrlEncodedString.IndexOf("&",lnStart);
 93			if (Index2 < 0)
 94				 return "";
 95
 96			string Result = wwHttpUtils.UrlDecode(  UrlEncodedString.Substring(lnStart,Index2 - lnStart) );
 97
 98			return Result;
 99		}
100
101	}
102}