Пример #1
0
        public static string RemoveQueryStringKeyFromUrl(Uri OriginalUri, string QsKeyToRemove)
        {
            return(Urls.RemoveQueryStringKeyFromUrl(OriginalUri, QsKeyToRemove));

            var uri = OriginalUri;

            var baseUrl = uri.AbsoluteUri.Replace(uri.Query, "");
            // var basePath = uri.AbsolutePath;

            //Anchor Tag
            var currAnchor = uri.Fragment.Replace("#", "");
            //var providedAnchor = NewAnchor.Replace("#", "");
            var newAnchor = "";

            if (currAnchor != "")
            {
                //    if (NewAnchor != "")
                //    {
                //        newAnchor = providedAnchor;
                //    }
                //    else
                //    {
                newAnchor = currAnchor;
                //}

                newAnchor = "#" + newAnchor;
            }
            //else
            //{
            //newAnchor = providedAnchor;
            //}

            //Query String Values
            var qs = GetQueryStringDictionary(uri.Query);

            if (qs.ContainsKey(QsKeyToRemove))
            {
                qs.Remove(QsKeyToRemove);
            }

            var allQs = AssembleQueryString(qs);

            //Build New Url
            var newUrl = string.Format("{0}?{1}{2}", baseUrl, allQs, newAnchor);

            newUrl = newUrl.Replace("?&", "?"); //Cleanup if all QS have been removed

            return(newUrl);
        }
Пример #2
0
        public static string CallUrl(string Url, string Method, string UserAgentString = "C# Application (compatible; MSIE 6.0; Windows NT 5.1)")
        {
            return(Urls.CallUrl(Url, Method, UserAgentString));

            //From http://you.arenot.me/2010/09/28/facebooks-graph-api-and-asp-net/
            string UserAgent = UserAgentString;
            int    _timeout  = 300000;

            HttpWebRequest  req = null;
            HttpWebResponse res = null;

            // Initialise the web request
            req        = (HttpWebRequest)WebRequest.Create(Url);
            req.Method = Method.Length > 0 ? Method : "POST";

            req.UserAgent = UserAgent;

            // if (Proxy != null) req.Proxy = Proxy;
            req.Timeout   = _timeout;
            req.KeepAlive = false;

            // This is needed in the Compact Framework
            // See for more details: http://msdn2.microsoft.com/en-us/library/1afx2b0f.aspx
            if (Method != "GET")
            {
                req.GetRequestStream().Close();
            }

            string responseString = String.Empty;

            try
            {
                // Get response from the internet
                res = (HttpWebResponse)req.GetResponse();
                using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                {
                    responseString = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                //TODO: Update using new code pattern:
                //var functionName = string.Format("{0}.GetMySQLDataSet", ThisClassName);
                //var msg = string.Format("");
                //Info.LogException("Functions.CallUrl", ex);
            }

            return(responseString);
        }
Пример #3
0
        public static Uri Rewrite(this Uri uri, string path)
        {
            return(Urls.Rewrite(uri, path));

            //Copied from https://github.com/umbraco/Umbraco-CMS/blob/d50e49ad37fd5ca7bad2fd6e8fc994f3408ae70c/src/Umbraco.Core/UriExtensions.cs

            if (path.StartsWith("/") == false)
            {
                throw new ArgumentException("Path must start with a slash.", "path");
            }

            return(uri.IsAbsoluteUri
                ? new Uri(uri.GetLeftPart(UriPartial.Authority) + path + uri.Query)
                : new Uri(path + uri.GetSafeQuery(), UriKind.Relative));
        }
Пример #4
0
        public static string GetSafeQueryString(System.Web.HttpRequestBase Request, string QueryStringKey, string DefaultIfMissing = "")
        {
            return(Urls.GetSafeQueryString(Request, QueryStringKey, DefaultIfMissing));

            var returnVal = DefaultIfMissing;

            var qsVal = Request.QueryString[QueryStringKey];

            if (!string.IsNullOrEmpty(qsVal))
            {
                returnVal = qsVal;
            }

            return(returnVal);
        }
Пример #5
0
        public static bool GetSafeQueryBool(System.Web.HttpRequestBase Request, string QueryStringKey, bool DefaultIfMissing = false)
        {
            return(Urls.GetSafeQueryBool(Request, QueryStringKey, DefaultIfMissing));

            var returnVal = DefaultIfMissing;

            var qsVal = Request.QueryString[QueryStringKey];

            if (!string.IsNullOrEmpty(qsVal))
            {
                if (qsVal.ToLower() == "true")
                {
                    returnVal = true;
                }
            }
            return(returnVal);
        }
Пример #6
0
        public static string AssembleQueryString(Dictionary <string, string> QueryStringDictionary)
        {
            return(Urls.AssembleQueryString(QueryStringDictionary));

            var allQs = "&";

            foreach (var qs in QueryStringDictionary)
            {
                if (qs.Value != "")
                {
                    //add qs with value
                    allQs = string.Format("{0}&{1}={2}", allQs, qs.Key, qs.Value);
                }
            }
            allQs = allQs.Replace("&&", "");

            return(allQs);
        }
Пример #7
0
        public static int GetSafeQueryInt(System.Web.HttpRequestBase Request, string QueryStringKey, int DefaultIfMissing = 0)
        {
            return(Urls.GetSafeQueryInt(Request, QueryStringKey, DefaultIfMissing));

            var returnVal = DefaultIfMissing;

            var qsVal = Request.QueryString[QueryStringKey];

            if (!string.IsNullOrEmpty(qsVal))
            {
                var isInt = Int32.TryParse(qsVal, out int intVal);
                if (isInt)
                {
                    returnVal = intVal;
                }
            }
            return(returnVal);
        }
Пример #8
0
        public static Dictionary <string, string> GetQueryStringDictionary(string Query)
        {
            return(Urls.GetQueryStringDictionary(Query));

            var returnDict = new Dictionary <string, string>();

            if (Query != "")
            {
                var splitString = Query.Replace("?", "").Split('&');

                foreach (var pairString in splitString)
                {
                    var pair = pairString.Split('=');
                    returnDict.Add(pair[0], pair[1]);
                }
            }

            return(returnDict);
        }
Пример #9
0
        public static string AppendQueryStringToUrl(Uri OriginalUri, string QsKey, string QsValue, string AppendMatchingTagDelim = "", string NewAnchor = "")
        {
            return(Urls.AppendQueryStringToUrl(OriginalUri, QsKey, QsValue, AppendMatchingTagDelim, NewAnchor));

            var uri = OriginalUri;

            var baseUrl  = uri.AbsoluteUri;
            var basePath = uri.AbsolutePath;

            //Anchor Tag
            var currAnchor     = uri.Fragment.Replace("#", "");
            var providedAnchor = NewAnchor.Replace("#", "");
            var newAnchor      = "";

            if (currAnchor != "")
            {
                if (NewAnchor != "")
                {
                    newAnchor = providedAnchor;
                }
                else
                {
                    newAnchor = currAnchor;
                }

                newAnchor = "#" + newAnchor;
            }
            else
            {
                newAnchor = providedAnchor;
            }

            //Query String Values
            var qs              = GetQueryStringDictionary(uri.Query);
            var allQs           = "&";
            var newKeyProcessed = false;

            foreach (var item in qs)
            {
                if (item.Key == QsKey)
                {
                    //matches the new KV - process
                    newKeyProcessed = true;
                    if (QsValue != "")
                    {
                        if (AppendMatchingTagDelim != "")
                        {
                            var currVals = item.Value.Split(AppendMatchingTagDelim.ToCharArray());
                            if (!currVals.Contains(QsValue))
                            {
                                //append value to existing
                                allQs = string.Format("{0}&{1}={2}{3}{4}", allQs, QsKey, item.Value, AppendMatchingTagDelim, QsValue);
                            }
                        }
                        else
                        {
                            //replace value
                            allQs = string.Format("{0}&{1}={2}", allQs, QsKey, QsValue);
                        }
                    }
                    else
                    {
                        //remove tag - skip
                    }
                }
                else
                {
                    //Just append it as-is
                    allQs = string.Format("{0}&{1}={2}", allQs, item.Key, item.Value);
                }
            }

            if (!newKeyProcessed && QsValue != "")
            {
                //Wasn't in current URI, needs to be added
                allQs = string.Format("{0}&{1}={2}", allQs, QsKey, QsValue);
            }

            allQs = allQs.Replace("&&", "");

            //Build New Url
            var newUrl = string.Format("{0}?{1}{2}", basePath, allQs, newAnchor);

            newUrl = newUrl.Replace("?&", "?"); //Cleanup if all QS have been removed
            //uri.Rewrite(newUrl);

            return(newUrl);
        }
Пример #10
0
        public static string CallUrl(string Url)
        {
            return(Urls.CallUrl(Url));

            return(CallUrl(Url, "GET"));
        }