Julian Bechtold Julian Bechtold -3 years ago 143
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


you will automaticaly get redirected to the latest version


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,


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");
            Console.WriteLine("Congrats! You are using the newest Version of programname!");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download