联系一个网站并以c#中的字符串形式获得重定向url

本文关键字:字符串 url 重定向 一个 网站 联系 | 更新日期: 2023-09-27 18:03:46

我注意到如果你访问你的git仓库的url,例如
https://github.com/user/project/releases/latest
您将自动重定向到最新版本
https://github.com/user/project/releases/tag/1.3

是否有可能在c#中联系上述url,被重定向并将响应url作为字符串?

这样,我只需要做一个string.Split('/'),并采取最新的子字符串快速和简单的版本检查我的应用程序。

最诚挚的问候,朱利安

联系一个网站并以c#中的字符串形式获得重定向url

这是我想到的(解决):

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!");
        }
    }