Inheritance: System.Net.WebClient
コード例 #1
0
ファイル: CodeplexReleases.cs プロジェクト: Stevolk/SEToolbox
        public static CodeplexReleases CheckForUpdates()
        {
            if (GlobalSettings.Default.AlwaysCheckForUpdates.HasValue && !GlobalSettings.Default.AlwaysCheckForUpdates.Value)
                return null;

#if DEBUG
            // Skip the load check, as it may take a few seconds during development.
            if (Debugger.IsAttached)
                return null;
#endif

            var currentVersion = GlobalSettings.GetAppVersion();
            string webContent;
            string link;

            // Create the WebClient with Proxy Credentials.
            using (var webclient = new MyWebClient())
            {
                try
                {
                    if (webclient.Proxy != null)
                    {
                        // For Proxy servers on Corporate networks.
                        webclient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                    }
                }
                catch
                {
                    // Reading from the Proxy may cause a network releated error.
                    // Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section. 
                }
                webclient.Headers.Add(HttpRequestHeader.UserAgent, string.Format("Mozilla/5.0 ({0}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36", Environment.OSVersion)); // Crude useragent.
                webclient.Headers.Add(HttpRequestHeader.Referer, string.Format("https://setoolbox.codeplex.com/updatecheck?current={0}", currentVersion));

                try
                {
                    webContent = webclient.DownloadString(UpdatesUrl);
                }
                catch
                {
                    // Ignore any errors.
                    // If it cannot connect, then there may be an intermittant connection issue, either with the internet, or codeplex (which has happened before).
                    return null;
                }

                if (string.IsNullOrEmpty(webContent))
                    return null;

                // link should be in the form "http://setoolbox.codeplex.com/releases/view/120855"
                link = webclient.ResponseUri == null ? null : webclient.ResponseUri.AbsoluteUri;
            }

            // search for html in the form:  <h1 class="page_title wordwrap">SEToolbox 01.025.021 Release 2</h1>
            var match = Regex.Match(webContent, @"\<h1 class=\""(?:[^\""]*)\""\>(?<title>(?:[^\<\>\""]*?))\s(?<version>[^\<\>\""]*)\<\/h1\>");

            if (!match.Success)
                return null;

            var item = new CodeplexReleases { Link = link, Version = GetVersion(match.Groups["version"].Value) };
            Version ignoreVersion;
            Version.TryParse(GlobalSettings.Default.IgnoreUpdateVersion, out ignoreVersion);
            if (item.Version > currentVersion && item.Version != ignoreVersion)
                return item;

            return null;
        }
コード例 #2
0
        public static ApplicationRelease CheckForUpdates(CodeRepositoryType repositoryType,  string updatesUrl)
        {
            if (GlobalSettings.Default.AlwaysCheckForUpdates.HasValue && !GlobalSettings.Default.AlwaysCheckForUpdates.Value)
                return null;

#if DEBUG
            // Skip the load check, as it may take a few seconds during development.
            if (Debugger.IsAttached)
                return null;
#endif

            var currentVersion = GlobalSettings.GetAppVersion();
            string webContent;
            string link;

            // Create the WebClient with Proxy Credentials.
            using (var webclient = new MyWebClient())
            {
                try
                {
                    if (webclient.Proxy != null)
                    {
                        // For Proxy servers on Corporate networks.
                        webclient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                    }
                }
                catch
                {
                    // Reading from the Proxy may cause a network releated error.
                    // Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section. 
                }
                webclient.Headers.Add(HttpRequestHeader.UserAgent, string.Format("Mozilla/5.0 ({0}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36", Environment.OSVersion)); // Crude useragent.

                try
                {
                    webContent = webclient.DownloadString(updatesUrl);
                }
                catch
                {
                    // Ignore any errors.
                    // If it cannot connect, then there may be an intermittant connection issue, either with the internet, or the website itself.
                    return null;
                }

                if (string.IsNullOrEmpty(webContent))
                    return null;

                // link should be in the form "http://setoolbox.codeplex.com/releases/view/120855"
                link = webclient.ResponseUri == null ? null : webclient.ResponseUri.AbsoluteUri;
            }

            string pattern = "";
            if (repositoryType == CodeRepositoryType.CodePlex)
                pattern = CodePlexPattern;
            if (repositoryType == CodeRepositoryType.GitHub)
                pattern = GitHubPattern;

            var match = Regex.Match(webContent, pattern);

            if (!match.Success)
                return null;

            var item = new ApplicationRelease { Link = link, Version = GetVersion(match.Groups["version"].Value) };
            Version ignoreVersion;
            Version.TryParse(GlobalSettings.Default.IgnoreUpdateVersion, out ignoreVersion);
            if (item.Version > currentVersion && item.Version != ignoreVersion)
                return item;

            return null;
        }