这是我使用应用程序包名称从Playstore获取版本号的功能。
public async Task<string> GetLatestVersionNumber(string appName)
{
if (string.IsNullOrWhiteSpace(appName))
{
throw new ArgumentNullException(nameof(appName));
}
var version = string.Empty;
var url = $"https://play.google.com/store/apps/details?id={appName}&hl=en";
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
using (var handler = new HttpClientHandler())
{
using (var client = new HttpClient(handler))
{
using (var responseMsg = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead))
{
if (!responseMsg.IsSuccessStatusCode)
{
//throw new LatestVersionException($"Error connecting to the Play Store. Url={url}.");
}
try
{
var content = responseMsg.Content == null ? null : await responseMsg.Content.ReadAsStringAsync();
var versionMatch = Regex.Match(content, "<div[^>]*>Current Version</div><span[^>]*><div><span[^>]*>(.*?)<").Groups[1];
if (versionMatch.Success)
{
version = versionMatch.Value.Trim();
}
}
catch (Exception e)
{
// throw new LatestVersionException($"Error parsing content from the Play Store. Url={url}.", e);
}
}
}
}
}
return version;
}
我的应用程序版本号是49 版本名称是1.0.103 在内容变量中,我有一个大型的html代码,我发现
<div class=\"IQ1z0d\"><span class=\"htlgb\">10+</span></div></span></div><div class=\"hAyfc\"><div class=\"BgcNfc\">Current Version</div><span class=\"htlgb\"><div class=\"IQ1z0d\"><span class=\"htlgb\">1.2.103</span></div></span></div><div class=\"hAyfc\"><div class=\"BgcNfc\">Requires Android</div><span class=\"htlgb\">
在versionMatch变量上,我变得空白。如何解决此问题?
答案 0 :(得分:-1)
您可以使用以下代码获取应用程序的最新版本,
为您的应用程序URL创建一个变量,
public const string _AndroidPackage = "https://play.google.com/store/apps/details?id={your app package name}";
使用以下代码从Play商店获取版本号,
if (Device.RuntimePlatform == Device.Android)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/soap+xml"));
client.BaseAddress = new Uri(RESTKeys._AndroidPackage);
HttpResponseMessage response = await client.GetAsync(RESTKeys._AndroidPackage);
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
if (result.Contains("Current Version"))
{
int startIndex = result.IndexOf("Current Version</div>") + 66;
result = result.Substring(startIndex);
int endIndex = result.IndexOf("</span>");
result = result.Substring(0, endIndex);
return result.Trim();
}
else
{
return "";
}
}
}
}
catch (Exception)
{
return "";
}
}