Exemplo n.º 1
0
        /// <summary>
        /// Gets a resource, using a Uniform Resource Identifier (or Locator).
        /// </summary>
        /// <param name="Uri">URI</param>
        /// <param name="TimeoutMs">Timeout, in milliseconds. (Default=60000)</param>
        /// <param name="Headers">Optional headers. Interpreted in accordance with the corresponding URI scheme.</param>
        /// <exception cref="InvalidOperationException">No <see cref="HttpxProxy"/> set in the HTTPX <see cref="Types"/> module parameter.</exception>
        /// <exception cref="ArgumentException">If the <paramref name="Uri"/> parameter is invalid.</exception>
        /// <exception cref="ArgumentException">If the object response be decoded.</exception>
        /// <exception cref="ConflictException">If an approved presence subscription with the remote entity does not exist.</exception>
        /// <exception cref="ServiceUnavailableException">If the remote entity is not online.</exception>
        /// <exception cref="TimeoutException">If the request times out.</exception>
        /// <exception cref="OutOfMemoryException">If resource too large to decode.</exception>
        /// <exception cref="IOException">If unable to read from temporary file.</exception>
        /// <returns>Decoded object.</returns>
        public async Task <object> GetAsync(Uri Uri, int TimeoutMs, params KeyValuePair <string, string>[] Headers)
        {
            KeyValuePair <string, TemporaryFile> Rec = await this.GetTempFileAsync(Uri, TimeoutMs, Headers);

            string        ContentType = Rec.Key;
            TemporaryFile File        = Rec.Value;

            try
            {
                if (File is null)
                {
                    return(null);
                }

                File.Position = 0;

                if (File.Length > int.MaxValue)
                {
                    throw new OutOfMemoryException("Resource too large.");
                }

                int    Len = (int)File.Length;
                byte[] Bin = new byte[Len];
                if (await File.ReadAsync(Bin, 0, Len) != Len)
                {
                    throw new IOException("Unable to read from file.");
                }

                return(InternetContent.Decode(ContentType, Bin, Uri));
            }
            finally
            {
                File?.Dispose();
            }
        }