Example #1
0
        /// <summary>
        /// Send a GET request to the specified <see cref="Uri"/>.
        /// </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>
        /// <exception cref="ArgumentNullException">The <see cref="requestUri"/> parameter is null.</exception>
        public async Task <CefNetWebRequest> GetAsync(Uri requestUri, Uri referrerUri, CefReferrerPolicy referrerPolicy, NameValueCollection headers, CancellationToken cancellationToken)
        {
            if (requestUri is null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            var r = new CefRequest();

            r.Flags = (int)this.RequestFlags;
            r.Url   = requestUri.AbsoluteUri;
            if (referrerUri != null)
            {
                r.SetReferrer(referrerUri.AbsoluteUri, referrerPolicy);
            }
            if (headers != null && headers.Count > 0)
            {
                using (var map = new CefStringMultimap())
                {
                    map.Add(headers);
                    r.SetHeaderMap(map);
                }
            }

            var request = new CefNetWebRequest(this);
            await request.SendAsync(r, _context, cancellationToken);

            return(request);
        }
Example #2
0
 /// <summary>
 /// Tests that the request completed successfully.
 /// </summary>
 /// <param name="request">The request for testing.</param>
 protected virtual void AssertSuccess(CefNetWebRequest request)
 {
     if (request.RequestStatus != CefUrlRequestStatus.Success)
     {
         throw new WebException("Error: " + request.RequestError.ToString());
     }
 }
Example #3
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 #4
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());
        }