当前位置:首页 > 开发 > 编程语言 > C# > 正文

C#下的Uri.Builder

发表于: 2012-08-20   作者:dai_lm   来源:转载   浏览:
摘要: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace Uri { class Builder { private string scheme = "http&
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Uri
{
    class Builder
    {
        private string scheme = "http";
        private string host = "";
        private StringBuilder path;
        private Boolean endwithSlash = false;
        private IDictionary<string, string> param = null;

        public Builder()
        {
            scheme = "http";
            host = "";
            path = new StringBuilder("");
            endwithSlash = false;

            param = new Dictionary<string, string>();
        }

        public Builder SetScheme(string scheme)
        {
            this.scheme = scheme;

            return this;
        }

        public Builder SetHost(string host)
        {
            this.host = host;

            return this;
        }

        public Builder Path(string path)
        {
            return AppendPath(ParsePath(path));
        }

        public Builder EncodePath(string path)
        {
            return AppendEncodePath(ParsePath(path));
        }

        public Builder AppendPath(string path)
        {
            if (path.Length > 0)
            {
                // start with '/'
                if (!endwithSlash && !path.StartsWith("/"))
                    this.path.Append("/");

                this.path.Append(path);

                if (path.EndsWith("/"))
                    endwithSlash = true;
                else
                    endwithSlash = false;
            }

            return this;
        }

        public Builder AppendEncodePath(string path)
        {
            return AppendPath(Encode(path));
        }

        private string ParsePath(string path)
        {
            int posScheme = path.IndexOf("://");
            if (posScheme > 0)
            {
                // get scheme
                scheme = path.Substring(0, posScheme);
                path = path.Substring(posScheme + 3);
            }

            int posHost = path.IndexOf("/");
            if (posHost == -1)
            {
                // not found '/'
                host = path;
                path = "";
            }
            else
            {
                host = path.Substring(0, posHost);
                path = path.Substring(posHost + 1);
            }

            return path;
        }

        public Builder AppendParameter(string key, string value)
        {
            param.Add(key, value);

            return this;
        }

        public Builder AppendEncodeParameter(string key, string value)
        {
            return AppendParameter(key, Encode(value));
        }

        public string ToString()
        {
            // generate URL string
            StringBuilder url = new StringBuilder("");

            // scheme
            url.Append(scheme).Append("://");

            // host
            url.Append(host);

            // path
            if (path.Length > 0)
                url.Append(path);

            // param
            if (param.Count > 0)
            {
                string pre = "?";
                foreach (KeyValuePair<string, string> p in param)
                {
                    url.Append(pre);

                    if (p.Value != null)
                        url.AppendFormat("{0}={1}", p.Key, p.Value);
                    else
                        url.Append(p.Key);

                    pre = "&";
                }
            }

            return url.ToString();
        }

        private string Encode(string value)
        {
            return HttpUtility.UrlEncode(value);
        }

        public Builder Parse(string url)
        {
            // seperate path and parameters
            string[] div = url.Split(new string[] { "?" }, 2, StringSplitOptions.None);
            Path(div[0]);
            if (div.Length == 2)
                ParseParameters(div[1]);

            return this;
        }

        private void ParseParameters(string param)
        {
            if (param.Length == 0)
                return;

            string[] para = param.Split(new string[] { "&" }, StringSplitOptions.None);
            foreach (string p in para)
            {
                string[] val = p.Split(new string[] { "=" }, 2, StringSplitOptions.None);
                if (val.Length == 2)
                    AppendParameter(val[0], val[1]);
                else
                    AppendParameter(val[0], null);
            }
        }
    }
}

C#下的Uri.Builder

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号