Пример #1
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var jObj = JObject.Load(reader);

                var result = new SingleUriCookieContainer()
                {
                    Uri               = new Uri(jObj["Uri"].Value <string>()),
                    Capacity          = jObj["Capacity"].Value <int>(),
                    MaxCookieSize     = jObj["MaxCookieSize"].Value <int>(),
                    PerDomainCapacity = jObj["PerDomainCapacity"].Value <int>()
                };

                var cookieList = jObj["Cookies"].ToList();

                foreach (var cookie in cookieList)
                {
                    result.Add(
                        new Cookie
                    {
                        Comment  = cookie["Comment"].Value <string>(),
                        HttpOnly = cookie["HttpOnly"].Value <bool>(),
                        Discard  = cookie["Discard"].Value <bool>(),
                        Domain   = cookie["Domain"].Value <string>(),
                        Expired  = cookie["Expired"].Value <bool>(),
                        Expires  = cookie["Expires"].Value <DateTime>(),
                        Name     = cookie["Name"].Value <string>(),
                        Path     = cookie["Path"].Value <string>(),
                        Port     = cookie["Port"].Value <string>(),
                        Secure   = cookie["Secure"].Value <bool>(),
                        Value    = cookie["Value"].Value <string>(),
                        Version  = cookie["Version"].Value <int>(),
                    });
                }

                return(result);
            }
Пример #2
0
        /// <summary>
        /// A resumable, simultaneous file downloader and reader.
        /// </summary>
        /// <param name="saveFilePath">Path to a location on disk to save the downloaded data from <paramref name="uri"/></param>
        /// <param name="uri">Http(s) address of the file to download.</param>
        /// <param name="writePosition">The position in <paramref name="uri"/> to begin downloading.</param>
        /// <param name="requestHeaders">Http headers to be sent to the server with the <see cref="HttpWebRequest"/>.</param>
        /// <param name="cookies">A <see cref="SingleUriCookieContainer"/> with cookies to send with the <see cref="HttpWebRequest"/>. It will also be populated with any cookies set by the server. </param>
        public NetworkFileStream(string saveFilePath, Uri uri, long writePosition = 0, WebHeaderCollection requestHeaders = null, SingleUriCookieContainer cookies = null)
        {
            ArgumentValidator.EnsureNotNullOrWhiteSpace(saveFilePath, nameof(saveFilePath));
            ArgumentValidator.EnsureNotNullOrWhiteSpace(uri?.AbsoluteUri, nameof(uri));
            ArgumentValidator.EnsureGreaterThan(writePosition, nameof(writePosition), -1);

            if (!Directory.Exists(Path.GetDirectoryName(saveFilePath)))
            {
                throw new ArgumentException($"Specified {nameof(saveFilePath)} directory \"{Path.GetDirectoryName(saveFilePath)}\" does not exist.");
            }

            SaveFilePath    = saveFilePath;
            Uri             = uri;
            WritePosition   = writePosition;
            RequestHeaders  = requestHeaders ?? new WebHeaderCollection();
            CookieContainer = cookies ?? new SingleUriCookieContainer {
                Uri = uri
            };

            _writeFile = new FileStream(SaveFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)
            {
                Position = WritePosition
            };

            _readFile = new FileStream(SaveFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            SetUriForSameFile(uri);
        }