Julian Bechtold Julian Bechtold - 2 months ago 13
C# Question

Contact a website and obtain the redirect url as String in c#

I noticed if you visit the url of your git repository, eg

https://github.com/user/project/releases/latest

you will automaticaly get redirected to the latest version

https://github.com/user/project/releases/tag/1.3

Is there a possibility in c# to contact the above url, be redirected and take the response url as string?

This way I only have to do a string.Split('/') and take the latest substring for a fast and easy versioncheck of my application.

Best Regards,

Julian

Answer Source

This is what I came up with (solved):

static void Versioncheck (double currentVersion)
    {
        //Get web response of git repository (/latest will forward you to the latest version)
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://github.com/[user]/[Project]/releases/latest");
        req.Method = "HEAD";
        req.AllowAutoRedirect = true;
        WebResponse response = (HttpWebResponse)req.GetResponse();
        //split the parts of the responseuri
        string responseUri = response.ResponseUri.ToString();
        string[] uriParts = responseUri.Split('/');
        //compare the latest part of the versionuri (version number) with the currect program version
        if (Convert.ToDouble(uriParts.Last(), CultureInfo.InvariantCulture) > currentVersion)
        {
            Console.WriteLine("Version " + uriParts.Last() + " is available! You can get the latest Version here:");
            Console.WriteLine("Project download page");
        }
        else
        {
            Console.WriteLine("Congrats! You are using the newest Version of programname!");
        }
    }