简体   繁体   中英

System.FormatException: Invalid format. While trying to add a Header Key

I have a function where I call an API, to add a key authorization to the response header.

That is : _httpClient.DefaultRequestHeaders.Add("Authorization", accessToken);

However I have a function, where I will repetedly call the same function until I get all results based on a predefined Page Size.

Here how ever when the first time the function is called the above function is working perfectly, however subsequent recursive calls to the same function, throw an error System.FormatException: Invalid format.

Stack Trace :

System.FormatException: Invalid format.
  at System.Net.Http.Headers.HttpHeaders.AddInternal (System.String name, IEnumerable`1 values, System.Net.Http.Headers.HeaderInfo headerInfo, Boolean ignoreInvalid) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, IEnumerable`1 values) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.String value) [0x00000] in <filename unknown>:0 
  at  (System.String verb, System.String url, System.String parameters) [0x00041] in 

The defaultheaderrequest was already having same Key added to it in a previous request.

Hence I would require those keys to clear it then add again using,

_httpClient.DefaultRequestHeaders.Clear();

What a waste of an Exception message it is. Couldn't there be a more appropriate exception message?

A total waste of developers' time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM