Exemple #1
0
        private void DownLog(FileLog fileLog)
        {
            //string p = "H:\\行运\\八方客来.rar";
            //string df = BaseApi.GetDomain() + SysApi.Download +
            //            HttpServerUtility.UrlTokenEncode(HttpUtility.UrlEncodeToBytes(p));
            var info = new DownloadInfo
            {
                Name     = fileLog.Name,
                Url      = BaseApi.GetDomain() + SysApi.Download + HttpServerUtility.UrlTokenEncode(HttpUtility.UrlEncodeToBytes(fileLog.Path)),
                SavePath = GetDownloadFileSavePath()
            };

            var message = Application.Current.Windows.Cast <Window>().FirstOrDefault(o => o.GetType() == typeof(Sys.View.DownloadList));

            if (message != null)
            {
                message.Activate();
                (SimpleIoc.Default.GetInstance <DownloadListViewModel>()).AddDownload(info);
                return;
            }
            Task.Run(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    Common.ControlHelper.OpenWindow("CatchingFire.Area.Sys.View.DownloadList", null, false);
                    (SimpleIoc.Default.GetInstance <DownloadListViewModel>()).AddDownload(info);
                }));
            });
        }
Exemple #2
0
        private async void Init()
        {
            try
            {
                if (Common.Instance.LogType == "LocalLog")
                {
                    var bytes = File.ReadAllBytes(Common.Instance.Parameter + "");
                    Value += Encoding.Default.GetString(bytes);
                    return;
                }
                var url = BaseApi.GetDomain() + SysApi.Download + Common.Instance.Parameter;
                using (var httpClient = HttpClientUtil.CreateHttpClient())
                {
                    var response = await httpClient.GetAsync(url);

                    var contentType = response.Content.Headers.ContentType;
                    if (string.IsNullOrEmpty(contentType.CharSet))
                    {
                        contentType.CharSet = "utf-8";
                    }
                    long contentLength = -1;
                    if (response.Content.Headers.ContentLength != null)
                    {
                        contentLength = response.Content.Headers.ContentLength.Value;
                    }

                    using (var stream = await response.Content.ReadAsStreamAsync())
                    {
                        var readLength = 10 * 1024;
                        if (readLength != -1 && contentLength < readLength)
                        {
                            readLength = (int)contentLength;
                        }
                        var bytes = new byte[readLength];
                        while (((stream.Read(bytes, 0, readLength))) > 0)
                        {
                            Value         += Encoding.Default.GetString(bytes);
                            contentLength -= readLength;
                            if (contentLength <= 0)
                            {
                                break;
                            }
                            readLength = Math.Min((int)contentLength, readLength);
                            bytes      = new byte[readLength];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误", ex.Message, MessageBoxButton.OK);
                Utility.Logger.LogHelper.Error("下载文件出错", ex);
            }
        }
Exemple #3
0
        public static HttpClient CreateHttpClient()
        {
            var handler = new HttpClientHandler()
            {
                //CookieContainer = cookieContainer,
                UseCookies             = true,
                AllowAutoRedirect      = true,
                AutomaticDecompression = DecompressionMethods.GZip
                                         //UseProxy = true,
                                         //Proxy = new WebProxy("218.64.147.211", 9000)
            };
            var httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri(BaseApi.GetDomain()), Timeout = _timeOut
            };

            httpClient.DefaultRequestHeaders.Add("Accept", _accept);
            httpClient.DefaultRequestHeaders.Add("Accept-Language", _acceptLanguage);
            httpClient.DefaultRequestHeaders.Add("Accept-Encoding", _acceptEncoding);
            httpClient.DefaultRequestHeaders.Add("User-Agent", _userAgent);
            httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
            httpClient.DefaultRequestHeaders.Add("Accept-Charset", _charset);
            return(httpClient);
        }