Esempio n. 1
0
        public ElementDescription GetElementDescriptionFromSiteUri(string siteUri)
        {
            try
            {
                var request = WebRequest.Create(siteUri);
                request.Timeout = (int)Timeout.TotalMilliseconds;
                var response    = request.GetResponse();
                var linksString = response.Headers["Link"];
                var links       = HttpWebLink.ParseLinks(linksString);
                var link        = links.FirstOrDefault(x => x.Relation == APIHttpHeaderRelation);

                if (link == null)
                {
                    return(null);
                }

                var uri = link.Uri;
                var elementDescription = ElementDescription.ParseUri(uri);

                return(elementDescription);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 2
0
        public static HttpWebLink ParseLink(string linkString)
        {
            var link = new HttpWebLink();

            var leftAngledParenthesis = linkString.IndexOf('<');
            var rightAngledParenthesis = linkString.IndexOf('>');

            if (leftAngledParenthesis >= 0 && rightAngledParenthesis >= 0)
            {
                link.Uri = linkString.Substring(leftAngledParenthesis + 1, rightAngledParenthesis - leftAngledParenthesis - 1);
            }

            linkString = linkString.Substring(rightAngledParenthesis + 1);
            var parameters = linkString.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var parameter in parameters)
            {
                var splits = parameter.Split(new[] { '=' }, 2);
                if (splits.Length == 2)
                {
                    var parameterType = splits[0];
                    var parameterValue = splits[1].Trim('"');

                    switch (parameterType)
                    {
                        case "rel":
                            link.Relation = parameterValue;
                            break;
                        case "anchor":
                            link.Anchor = parameterValue;
                            break;
                        case "rev":
                            link.RelationTypes = parameterValue;
                            break;
                        case "hreflang":
                            link.Language = parameterValue;
                            break;
                        case "media":
                            link.Media = parameterValue;
                            break;
                        case "title":
                            link.Title = parameterValue;
                            break;
                        case "title*":
                            link.Titles = parameterValue;
                            break;
                        case "type":
                            link.Type = parameterValue;
                            break;
                    }
                }
            }

            return link;
        }
Esempio n. 3
0
        public static HttpWebLink ParseLink(string linkString)
        {
            var link = new HttpWebLink();

            var leftAngledParenthesis  = linkString.IndexOf('<');
            var rightAngledParenthesis = linkString.IndexOf('>');

            if (leftAngledParenthesis >= 0 && rightAngledParenthesis >= 0)
            {
                link.Uri = linkString.Substring(leftAngledParenthesis + 1, rightAngledParenthesis - leftAngledParenthesis - 1);
            }

            linkString = linkString.Substring(rightAngledParenthesis + 1);
            var parameters = linkString.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var parameter in parameters)
            {
                var splits = parameter.Split(new[] { '=' }, 2);
                if (splits.Length == 2)
                {
                    var parameterType  = splits[0];
                    var parameterValue = splits[1].Trim('"');

                    switch (parameterType)
                    {
                    case "rel":
                        link.Relation = parameterValue;
                        break;

                    case "anchor":
                        link.Anchor = parameterValue;
                        break;

                    case "rev":
                        link.RelationTypes = parameterValue;
                        break;

                    case "hreflang":
                        link.Language = parameterValue;
                        break;

                    case "media":
                        link.Media = parameterValue;
                        break;

                    case "title":
                        link.Title = parameterValue;
                        break;

                    case "title*":
                        link.Titles = parameterValue;
                        break;

                    case "type":
                        link.Type = parameterValue;
                        break;
                    }
                }
            }

            return(link);
        }