@@ -39,10 +39,7 @@ public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, st
3939 {
4040 if ( headers != null )
4141 {
42- foreach ( KeyValuePair < string , string > headerPair in headers )
43- {
44- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
45- }
42+ AddHeaders ( headers ) ;
4643 }
4744 using ( HttpResponseMessage response = Task . Run ( ( ) => _client . GetAsync ( url ) ) . Result )
4845 {
@@ -58,10 +55,7 @@ public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dict
5855 {
5956 if ( headers != null )
6057 {
61- foreach ( KeyValuePair < string , string > headerPair in headers )
62- {
63- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
64- }
58+ AddHeaders ( headers ) ;
6559 }
6660 using ( HttpResponseMessage response = await _client . GetAsync ( url ) . ConfigureAwait ( false ) )
6761 {
@@ -101,10 +95,7 @@ public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string met
10195 {
10296 if ( headers != null )
10397 {
104- foreach ( KeyValuePair < string , string > headerPair in headers )
105- {
106- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
107- }
98+ AddHeaders ( headers ) ;
10899 }
109100
110101 HttpRequestMessage message = new HttpRequestMessage ( new HttpMethod ( method ) , url )
@@ -125,10 +116,7 @@ public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string
125116 {
126117 if ( headers != null )
127118 {
128- foreach ( KeyValuePair < string , string > headerPair in headers )
129- {
130- _client . DefaultRequestHeaders . TryAddWithoutValidation ( headerPair . Key , headerPair . Value ) ;
131- }
119+ AddHeaders ( headers ) ;
132120 }
133121
134122 HttpRequestMessage message = new HttpRequestMessage ( new HttpMethod ( method ) , url )
@@ -176,6 +164,18 @@ private static WebHeaderCollection ConvertHeaders(HttpResponseHeaders headers)
176164 return newHeaders ;
177165 }
178166
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+
179179 private static HttpClientHandler CreateClientHandler ( ProxyConfig proxyConfig = null )
180180 {
181181 HttpClientHandler clientHandler = new HttpClientHandler
0 commit comments