Encode Url - CSharp Network

CSharp examples for Network:URL

Description

Encode Url

Demo Code

/*****************************************************************
 * Copyright (C) 2005-2006 Newegg Corporation
 * All rights reserved./* w  w w. j av a2s.  c  om*/
 * 
 * Author:   Jason Huang (jaosn.j.huang@newegg.com)
 * Create Date:  07/02/2008 15:12:41
 * Usage:
 *
 * RevisionHistory
 * Date         Author               Description
 * 
*****************************************************************/
using System.Text;

public class Main{
        private static string EncodeUrl(string strInput)
      {
         if (strInput == null)
         {
            return null;
         }
         if (strInput.Length == 0)
         {
            return string.Empty;
         }
         StringBuilder builder = new StringBuilder("", strInput.Length * 2);
         foreach (char ch in strInput)
         {
            if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch > '/') && (ch < ':')) || (((ch == '.') || (ch == '-')) || (ch == '_'))))
            {
               builder.Append(ch);
            }
            else if (ch > '\x007f')
            {
               builder.Append("%u" + TwoByteHex(ch));
            }
            else
            {
               builder.Append("%" + SingleByteHex(ch));
            }
         }
         return builder.ToString();
      }
}

Related Tutorials