Example #1
0
        /// <summary>
        /// Send a GET request to the specified <see cref="Uri"/> and return the response body
        /// as a string in an asynchronous operation.
        /// </summary>
        /// <param name="requestUri">The <see cref="Uri"/> the request is sent to.</param>
        /// <param name="referrerUri">
        /// The <see cref="Uri"/> of the referring site for a request. Can be null.
        /// </param>
        /// <param name="referrerPolicy">
        /// The policy for how the Referrer HTTP header value will be sent during request.
        /// </param>
        /// <param name="headers">
        /// A <see cref="NameValueCollection"/> containing header name/value pairs associated with a request. Can be null.
        /// </param>
        /// <param name="cancellationToken">
        /// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public async Task <string> GetStringAsync(Uri requestUri, Uri referrerUri, CefReferrerPolicy referrerPolicy, NameValueCollection headers, CancellationToken cancellationToken)
        {
            CefNetWebRequest request = await GetAsync(requestUri, referrerUri, referrerPolicy, headers, cancellationToken).ConfigureAwait(false);

            AssertSuccess(request);

            Stream responseStream = request.GetResponseStream();

            if (responseStream is null)
            {
                return(string.Empty);
            }

            CefResponse response = request.Response;

            if (response is null)
            {
                return(null);
            }

            Encoding encoding;

            try
            {
                encoding = Encoding.GetEncoding(response.Charset);
            }
            catch (ArgumentException)
            {
                encoding = this.DefaultEncoding;
            }
            return(new StreamReader(responseStream, encoding).ReadToEnd());
        }
Example #2
0
        /// <summary>
        /// Send a GET request to the specified <see cref="Uri"/> and return the response body
        /// as a <see cref="Stream"/> in an asynchronous operation.
        /// </summary>
        /// <param name="requestUri">The <see cref="Uri"/> the request is sent to.</param>
        /// <param name="referrerUri">
        /// The <see cref="Uri"/> of the referring site for a request. Can be null.
        /// </param>
        /// <param name="referrerPolicy">
        /// The policy for how the Referrer HTTP header value will be sent during request.
        /// </param>
        /// <param name="headers">
        /// A <see cref="NameValueCollection"/> containing header name/value pairs associated with a request. Can be null.
        /// </param>
        /// <param name="cancellationToken">
        /// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public async Task <Stream> GetStreamAsync(Uri requestUri, Uri referrerUri, CefReferrerPolicy referrerPolicy, NameValueCollection headers, CancellationToken cancellationToken)
        {
            if (requestUri is null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            CefNetWebRequest request = await GetAsync(requestUri, referrerUri, referrerPolicy, headers, cancellationToken).ConfigureAwait(false);

            AssertSuccess(request);

            return(request.GetResponseStream());
        }