Check if the header exists and replace it rather than add it again (#299)

This commit is contained in:
Florian Jäckel 2018-10-24 23:21:13 +02:00 committed by Jonas Dellinger
parent f94fbc82f3
commit 9859e9d4ae

View File

@ -39,10 +39,7 @@ namespace SpotifyAPI.Web
{
if (headers != null)
{
foreach (KeyValuePair<string, string> headerPair in headers)
{
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
AddHeaders(headers);
}
using (HttpResponseMessage response = Task.Run(() => _client.GetAsync(url)).Result)
{
@ -58,10 +55,7 @@ namespace SpotifyAPI.Web
{
if (headers != null)
{
foreach (KeyValuePair<string, string> headerPair in headers)
{
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
AddHeaders(headers);
}
using (HttpResponseMessage response = await _client.GetAsync(url).ConfigureAwait(false))
{
@ -101,10 +95,7 @@ namespace SpotifyAPI.Web
{
if (headers != null)
{
foreach (KeyValuePair<string, string> headerPair in headers)
{
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
AddHeaders(headers);
}
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
@ -125,10 +116,7 @@ namespace SpotifyAPI.Web
{
if (headers != null)
{
foreach (KeyValuePair<string, string> headerPair in headers)
{
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
AddHeaders(headers);
}
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
@ -176,6 +164,18 @@ namespace SpotifyAPI.Web
return newHeaders;
}
private void AddHeaders(Dictionary<string,string> headers)
{
foreach (KeyValuePair<string, string> headerPair in headers)
{
if (_client.DefaultRequestHeaders.Contains(headerPair.Key))
{
_client.DefaultRequestHeaders.Remove(headerPair.Key);
}
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
}
private static HttpClientHandler CreateClientHandler(ProxyConfig proxyConfig = null)
{
HttpClientHandler clientHandler = new HttpClientHandler