APP版本

取得已上架的APP的版本 (AppStoe & GooglePlay)

吳宗彥(Cliff) 2019/01/01 00:08:01
183

取得已上架的APP的版本 (AppStoe & GooglePlay)


簡介

分享查詢架上app的資訊 的方法

作者

吳宗彥(Cliff)


ANDROID(GOOGLE PLAY)
 
 string appName = $"com.nianticlabs.pokemongo";
            string country = $"zh_TW";
            string url = $"https://play.google.com/store/apps/details?id={appName}&hl={country}";
            string version = string.Empty;

            using (var request = new HttpRequestMessage(HttpMethod.Get, url))
            {
                using (var handler = new HttpClientHandler())
                {
                    using (var client = new HttpClient(handler))
                    {
                        using (var responseMsg = client.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result)
                        {
                            if (!responseMsg.IsSuccessStatusCode)
                            {
                                Console.WriteLine($"Error connecting to the Play Store. Url={url}.");
                            }

                            try
                            {
                                var content = responseMsg.Content == null ? null : responseMsg.Content.ReadAsStringAsync().Result;

                                //  var versionMatch = Regex.Match(content, "<div[^>]*>Current Version</div><span[^>]*><div[^>]*><span[^>]*>(.*?)<").Groups[1];
                                var versionMatch = Regex.Match(content, "<div[^>]*>目前版本</div><span[^>]*><div[^>]*><span[^>]*>(.*?)<").Groups[1];

                                if (versionMatch.Success)
                                {
                                    version = versionMatch.Value.Trim();
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine($"Error parsing content from the Play Store. Url={url}.", e);
                            }
                        }
                    }
                }
                Console.WriteLine("version:" + version);
 
補充說明
 
1.國家(區域) 當 string country = $"zh_TW"; 擷取網站內容中的 "Current Version"要換成"目前版本",
 
2.此方法是擷取app網頁資訊 , 如果網頁大幅改動請記得修正
  
 
iOS (AppStore)
 string appName = $"com.nianticlabs.pokemongo";
            string country = $"tw";
            var url = $"http://itunes.apple.com/lookup?bundleId={appName}&country={country}";
            var version = string.Empty;

            using (var request = new HttpRequestMessage(HttpMethod.Get, url))
            {
                using (var handler = new HttpClientHandler())
                {
                    using (var client = new HttpClient(handler))
                    {
                        using (var responseMsg = client.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result)
                        {
                            if (!responseMsg.IsSuccessStatusCode)
                            {
                                Console.WriteLine($"Error connecting to the App Store. Url={url}.");
                            }

                            try
                            {
                                var content = responseMsg.Content == null ? null : responseMsg.Content.ReadAsStringAsync().Result;

                                var appStoreItem = System.Json.JsonValue.Parse(content);

                                version = appStoreItem["results"][0]["version"];
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine($"Error parsing content from the App Store. Url={url}.", e);
                            }
                        }
                    }
                }
            }
       
            Console.WriteLine("version:"+ version);
 
補充說明:
 
 
2. api會傳回的json物件, 可以依內容擷取需要的app資訊
 
 
結語
 
如果有需求要取得線上app的資訊,可以選擇如本篇從架上獲取資訊,適合想呈現一個或多個app資訊又不想用自己維護資料時。
 
當然也可以使用自己的api 讓用戶端看到app相關資訊,如果資料量小相對會較好維護。
 
 
吳宗彥(Cliff)