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) {
                    var msg = Query.ExtractError(we.Response);
                    if (msg != null)
                    {
                        throw new QueryException(msg, we);
                    }
                    throw;
                }
            }
            finally {
                this._clientLock.Release();
            }
        }