@@ -39,10 +39,7 @@ public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, st
39
39
{
40
40
if ( headers != null )
41
41
{
42
- foreach ( KeyValuePair < string , string > headerPair in headers )
43
- {
44
- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
45
- }
42
+ AddHeaders ( headers ) ;
46
43
}
47
44
using ( HttpResponseMessage response = Task . Run ( ( ) => _client . GetAsync ( url ) ) . Result )
48
45
{
@@ -58,10 +55,7 @@ public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dict
58
55
{
59
56
if ( headers != null )
60
57
{
61
- foreach ( KeyValuePair < string , string > headerPair in headers )
62
- {
63
- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
64
- }
58
+ AddHeaders ( headers ) ;
65
59
}
66
60
using ( HttpResponseMessage response = await _client . GetAsync ( url ) . ConfigureAwait ( false ) )
67
61
{
@@ -101,10 +95,7 @@ public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string met
101
95
{
102
96
if ( headers != null )
103
97
{
104
- foreach ( KeyValuePair < string , string > headerPair in headers )
105
- {
106
- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
107
- }
98
+ AddHeaders ( headers ) ;
108
99
}
109
100
110
101
HttpRequestMessage message = new HttpRequestMessage ( new HttpMethod ( method ) , url )
@@ -125,10 +116,7 @@ public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string
125
116
{
126
117
if ( headers != null )
127
118
{
128
- foreach ( KeyValuePair < string , string > headerPair in headers )
129
- {
130
- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
131
- }
119
+ AddHeaders ( headers ) ;
132
120
}
133
121
134
122
HttpRequestMessage message = new HttpRequestMessage ( new HttpMethod ( method ) , url )
@@ -176,6 +164,18 @@ private static WebHeaderCollection ConvertHeaders(HttpResponseHeaders headers)
176
164
return newHeaders ;
177
165
}
178
166
167
+ private void AddHeaders ( Dictionary < string , string > headers )
168
+ {
169
+ foreach ( KeyValuePair < string , string > headerPair in headers )
170
+ {
171
+ if ( _client . DefaultRequestHeaders . Contains ( headerPair . Key ) )
172
+ {
173
+ _client . DefaultRequestHeaders . Remove ( headerPair . Key ) ;
174
+ }
175
+ _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
176
+ }
177
+ }
178
+
179
179
private static HttpClientHandler CreateClientHandler ( ProxyConfig proxyConfig = null )
180
180
{
181
181
HttpClientHandler clientHandler = new HttpClientHandler
0 commit comments