Exemple #1
0
        /// <summary>
        /// 获得网站的版本
        /// </summary>
        /// <returns></returns>
        public static string GetResourceVersion(out int code, out string err)
        {
            code = 0;
            err  = null;

            var client = new NetClient();
            var task   = client.RunRequestLoop(_ => client.Create <string>(HttpMethod.Get, NetworkEnvironment.UrlResourceVersion, ""), retryCount: 1);

            if (!task.IsValid() && task.Exception is SystemBusyException)
            {
                //网络繁忙,被封锁IP
                code = -2;
                err  = task.GetErrorMsg();

                return(null);
            }

            if (task != null && task.IsSuccess)
            {
                var text = task.Result;
                var m    = System.Text.RegularExpressions.Regex.Match(text, @"station_name\.js\?station_version=([\d\.]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    return(m.Groups[1].Value);
                }
            }
            code = -1;
            err  = task.GetErrorMsg();

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 获得站点JS的内容
        /// </summary>
        /// <returns></returns>
        public static string GetCityNameJsContent(out string err)
        {
            err = null;

            var client = new NetClient();
            var task   = client.RunRequestLoop(_ => client.Create <string>(HttpMethod.Get, NetworkEnvironment.UrlScriptCityName));

            if (task != null && task.IsSuccess)
            {
                return(task.Result);
            }

            err = task.GetErrorMsg();
            return(null);
        }
        public static void Init(IStartup startup, Action requireInitProxyType)
        {
            NetClientHandler.Init();
            if (NetworkConfiguration.Current.ProxyType == 3 && NetworkConfiguration.Current.ProxyClass == ProxyType.Socks5)
            {
                startup.Update("正在初始化代理服务器...");
                var instance = PolipoSocks5ToHttpProxyWrapper.Instance;
                instance.ParentSocksServerAddress = NetworkConfiguration.Current.Socks5ServerAddr;
                instance.ParentSocksServerPort    = NetworkConfiguration.Current.Socks5ServerPort;

                instance.Start().Wait();
                if (!instance.IsRunning)
                {
                    throw new Exception("未能初始化本地代理服务器");
                }

                startup.Update("正在测试代理是否正确...");
                var client = new NetClient();
                var str    = client.Create <string>(HttpMethod.Get, "https://kyfw.12306.cn/otn/", null).Send();
                if (!str.IsValid() || str.Result.IndexOf("铁路客户服务中心", StringComparison.Ordinal) == -1)
                {
                    instance.Stop().Wait();
                    throw new Exception("无法通过指定的Socks5代理服务器访问12306。");
                }
            }

            startup.Update("正在检测网络信息...");

            var host = NetworkConfiguration.Current.BaseUri.Host;
            var ip   = "";

            try
            {
                var addlist = System.Net.Dns.GetHostAddresses(host);
                if (!addlist.IsEmpty())
                {
                    ip = addlist[0].ToString();
                }
            }
            catch (Exception)
            {
            }

            GlobalEvents.BeforeRequest += (s, e) =>
            {
                Statistics.Current.WebRequestCount++;
            };
        }