My quest for world presidency

we intend to change the future and make it unchangeable!

C# code to convert a relative URL into an Absolute URL

Posted by roushdat on March 1, 2009

As part of the Foundations of Web Science Module, we were tasked to investigate the Web Graph by building up a crawler that goes through a page, extract links, follow these links and loop…and ultimately generate a graph showing each link as a node pointing to other nodes.

One of the minor obstacle we encountered was about dealing with relative links since extracting relative links from a webpage and trying to download the content pointed by that link will lead nowhere since stand-alone, the relative link is not really pointing to anything concrete in the Web.

Lazy as we are…my friend Deyan and I tried search for some c# codes online that can convert that…Couldn’t find it. So we had to code it ourselves and thought about sharing it with other lazy people. All you have to do is to create a library in c#, copy paste the code below, build it. In your project, you can then refer to the .dll file generated for that library.

/*A humble request, please include these comments if you use this code, part of it or it is helpful to you somehow.This piece of code is free to use, modify and share. Original Authors: Roushdat Elaheebocus & Deyan Karaivanov, University of Southampton, March 2009. Part of the Web Graph project, Foundations of Web Science. */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Web;

namespace RelToAbsConverter
{
    public class RelToAbs
    {

        public string GetAbsoluteUrl(string sourceUrl,string relUrl)
        {
            int countOfDots = relDotCount(relUrl);
            relUrl = relUrl.TrimStart('.');
            relUrl = relUrl.TrimStart('/');
            return convertRelToAbs(sourceUrl, relUrl, countOfDots);
        }

        private string convertRelToAbs(string sourceUrl, string relUrl, int countOfDots)
        {
            WebRequest req = WebRequest.Create(sourceUrl);
            Uri uri = req.RequestUri;
            int countOfParts = uri.Segments.Count();
            string collectedParts = "http://" + uri.Host;
            for (int i = 0; i < (countOfParts - countOfDots); i++)
            {
                collectedParts += uri.Segments[i];
            }
            collectedParts += relUrl;
            return collectedParts;
        }

        private int relDotCount(string relUrl)
        {
            int dotCount = 1;
            if (relUrl[0].Equals('.'))
            {
                dotCount = 1;
                for (int i = 1; i < 10; i++)
                {
                    if (relUrl[i].Equals('.'))
                        dotCount++;
                    else
                        break;
                }
            }
            return dotCount;
        }
    }
}

3 Responses to “C# code to convert a relative URL into an Absolute URL”

  1. David said

    Is this a joke?

    That code is ridiculously complex for what is a very simple task.

  2. roushdat said

    if you could share a simpler code in C#, that would be much appreciated :)

  3. no said

    Uri uri;
    if ( Uri.TryCreate(“http://base/”,”../relative”, out uri) ) doSomething(uri);

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>