Adding HttpClient headers throws a FormatException with some values

This happened in the context of encoding against Google Cloud Messaging, but applies elsewhere.

Consider the following:

var http = new HttpClient(); http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("key=XXX"); 

and

 var http = new HttpClient(); http.DefaultRequestHeaders.Add("Authorization", "key=XXX"); 

both of which throw a FormatException:

System.FormatException: invalid value key format = XXX '.

The solution is to remove the equal sign.

  • Digging into the reflector shows that there is a bunch of verification and analysis of the code that starts when you add a new header value. Why is all this necessary? Won't this client just go out of our way?

  • How do you exit the equal sign to change this value?

+42
c # google-cloud-messaging
Nov 02
source share
4 answers

Not sure if it’s still relevant, but I recently ran into this problem and was able to solve it by calling another method to add header information:

 var http = new HttpClient(); http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=XXX"); 
+87
Mar 06 '13 at 8:12
source

To your "why all this (parsing and checking) you need a" question ", the answer is this: it is defined in the HTTP standard.

In HTTP / 1.1 and RFC2617, the value of the authentication header (for example, WWW-Authentication and Authorization) consists of two parts: part of the scheme and part of the parameter .

For basic HTTP authentication, the scheme is "Basic", and the parameter can be something like "QWxhZGRpbjpvcGVuIHNlc2FtZQ ==", so the whole header becomes:

 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 

This is why your "key = XXX" does not pass the test because it lacks part of the circuit.

+11
Apr 12 '15 at 7:53
source

I got around this exception (my FormatException caused by commas in the value) by setting the authorization header as follows:

 var authenticationHeaderValue = new AuthenticationHeaderValue("some scheme", "some value"); client.DefaultRequestHeaders.Authorization = authenticationHeaderValue; 
+3
Dec 23 '15 at 5:39
source

This morning I asked a few questions, referring to an external API that does not meet the HTTP specification for writing.

As part of my post, they want Content-Type and Content-Disposition , which cannot be added to the HttpClient object. To add these headers, you need to create an HttpRequestMessage . There you need to add headers to the Content property.

 private HttpRequestMessage GetPostMessage(string uri, string contentType, string fileName, Stream content) { var request = new HttpRequestMessage { Content = new StreamContent(content), RequestUri = new Uri(uri), Method = HttpMethod.Post }; // contentType = "video/mp4" request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); //Need TryAddWithoutValidation because of the equals sign in the value. request.Content .Headers .TryAddWithoutValidation("Content-Disposition", $"attachment; filename=\"{Path.GetFileName(fileName)}\""); // If there is no equals sign in your content disposition, this will work: // request.Content.Headers.ContentDisposition = // new ContentDispositionHeaderValue($"attachment; \"{Path.GetFileName(fileName)}\""); return request; } 
0
Jul 11 '17 at 12:05
source



All Articles