Exemple #1
0
        private async Task <string> PerformRequestAsync(string address, Method method, string accept, string?contentType, string?body = null)
        {
            Debug.Print($"[{DateTime.UtcNow}] WEB SERVICE REQUEST: {method} {this.BaseUri}{address}");
            await this.ClientLock.WaitAsync();

            try {
                var wc = this.WebClient;
                if (this.BearerToken != null)
                {
                    wc.Headers.Set("Authorization", $"Bearer {this.BearerToken}");
                }
                if (contentType != null)
                {
                    wc.Headers.Set("Content-Type", contentType);
                }
                wc.Headers.Set("Accept", accept);
                wc.Headers.Set("User-Agent", this.FullUserAgent);
                wc.QueryString.Clear();
                try {
                    if (method == Method.GET)
                    {
                        return(await wc.DownloadStringTaskAsync(address).ConfigureAwait(false));
                    }
                    if (body != null)
                    {
                        Debug.Print($"[{DateTime.UtcNow}] => BODY ({contentType}): {body}");
                    }
                    return(await wc.UploadStringTaskAsync(address, method.ToString(), body ?? string.Empty).ConfigureAwait(false));
                }
                catch (WebException we) {
                    await Query.MaybeMapExceptionAsync(we);

                    throw;
                }
            }
            finally {
                this.ClientLock.Release();
            }
        }