联系网站并获取重定向 url as String in c#
Contact a website and obtain the redirect url as String in c#
我注意到如果您访问 git 存储库的 url,例如
https://github.com/user/project/releases/latest
您将自动重定向到最新版本
https://github.com/user/project/releases/tag/1.3
是否有可能在 c# 中联系上述 url,被重定向并将响应 url 作为字符串?
这样我只需要执行 string.Split('/') 并获取最新的子字符串即可快速轻松地检查我的应用程序的版本。
此致,
朱利安
这是我想出的(已解决):
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!");
}
}
我注意到如果您访问 git 存储库的 url,例如
https://github.com/user/project/releases/latest
您将自动重定向到最新版本
https://github.com/user/project/releases/tag/1.3
是否有可能在 c# 中联系上述 url,被重定向并将响应 url 作为字符串?
这样我只需要执行 string.Split('/') 并获取最新的子字符串即可快速轻松地检查我的应用程序的版本。
此致,
朱利安
这是我想出的(已解决):
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!");
}
}