Please let me know if this should be directed elsewhere.
I have been using the latest drop of the new HttpClient and I wanted to mention a bug and a couple of design issues.
Bug
Due to HttpClient still using the old "WebRequest" curmudgeon under the hood, I am running into the "This header must be modified using the appropriate property" exception when trying to set the
If-Modified-Since request header. Is this a known issue?
Design Issues
Http Response Headers - Why are the headers located in two different places within the API? They are located within the
HttpResponseMessage Headers property and the HttpContent Headers property. I do understand the distinction (The etags property doesn't make since within this distinction, though), but it is confusing to the end user. At the
end of the day, a http response header is a http response header; simplify the API discoverability, don't make it more difficult.
Http Request - This might be a little nit-picky, but why are the http response headers available on the
HttpRequestMessage class. For example, I can get to the
LastModified header off of the Content property; maybe it makes sense to simplify and reduce the API surface.
For the design issue, that's the way the new HTTP object model was designed, to have content be separate from the request or response (note that request and response headers are different, but they both use HttpContent, which has the same set of content
headers). Keeping the content headers with the content is a good separation of concerns, though it does lower the discoverability of the content headers slightly as a result.
System.ArgumentException: This header must be modified using the appropriate property or method.
Parameter name: name
Server stack trace:
at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)
at System.Net.WebHeaderCollection.Add(String name, String value)
at System.Net.Http.HttpClientHandler.SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
If this is the case, why would the Etag header not be exposed on HttpContent. How do you make the distinction about what belongs directly on the HttpResponseMessage and what belongs on HttpContent? Also, exposing these headers on HttpContent creates the
"extra api surface issue" on the HttpRequestMessage class.
If this is the case, why would the Etag header not be exposed on HttpContent.
An ETag is more than a content header, because it has to potentially take into account a whole bunch of non-content aspects of both the request and response.
davebettin
Member
313 Points
94 Posts
HttpClient Issues
Feb 24, 2012 09:24 PM|LINK
Please let me know if this should be directed elsewhere.
I have been using the latest drop of the new HttpClient and I wanted to mention a bug and a couple of design issues.
Bug
Due to HttpClient still using the old "WebRequest" curmudgeon under the hood, I am running into the "This header must be modified using the appropriate property" exception when trying to set the If-Modified-Since request header. Is this a known issue?
Design Issues
Http Response Headers - Why are the headers located in two different places within the API? They are located within the HttpResponseMessage Headers property and the HttpContent Headers property. I do understand the distinction (The etags property doesn't make since within this distinction, though), but it is confusing to the end user. At the end of the day, a http response header is a http response header; simplify the API discoverability, don't make it more difficult.
Http Request - This might be a little nit-picky, but why are the http response headers available on the HttpRequestMessage class. For example, I can get to the LastModified header off of the Content property; maybe it makes sense to simplify and reduce the API surface.
Thanks!
Dave
@dbettin
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: HttpClient Issues
Feb 24, 2012 09:40 PM|LINK
I'm not sure on the bug.
For the design issue, that's the way the new HTTP object model was designed, to have content be separate from the request or response (note that request and response headers are different, but they both use HttpContent, which has the same set of content headers). Keeping the content headers with the content is a good separation of concerns, though it does lower the discoverability of the content headers slightly as a result.
youssefm
Member
58 Points
14 Posts
Microsoft
Re: HttpClient Issues
Feb 24, 2012 10:31 PM|LINK
Hi Dave,
I can't seem to reproduce the bug you're hitting. Could you please provide the code you're using that's causing the issue?
davebettin
Member
313 Points
94 Posts
Re: HttpClient Issues
Feb 24, 2012 10:45 PM|LINK
Sure.
Here is the Exception:
System.ArgumentException: This header must be modified using the appropriate property or method.
Parameter name: name
Server stack trace:
at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)
at System.Net.WebHeaderCollection.Add(String name, String value)
at System.Net.Http.HttpClientHandler.SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
and this is the code:
_client.DefaultRequestHeaders.IfModifiedSince = _lastModified;
After this I call _client.GetAsync(uri) and it throws the above exception.
@dbettin
davebettin
Member
313 Points
94 Posts
Re: HttpClient Issues
Feb 24, 2012 10:53 PM|LINK
If this is the case, why would the Etag header not be exposed on HttpContent. How do you make the distinction about what belongs directly on the HttpResponseMessage and what belongs on HttpContent? Also, exposing these headers on HttpContent creates the "extra api surface issue" on the HttpRequestMessage class.
@dbettin
youssefm
Member
58 Points
14 Posts
Microsoft
Re: HttpClient Issues
Feb 24, 2012 11:10 PM|LINK
Thanks, Dave. Indeed, this seems like a bug. I'll make sure we track the issue.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: HttpClient Issues
Feb 24, 2012 11:55 PM|LINK
davebettin
Member
313 Points
94 Posts
Re: HttpClient Issues
Feb 25, 2012 12:05 AM|LINK
Awesome, thanks. If you decide to put the source up on Github, I would be happy to send a pull request. :)
@dbettin
davebettin
Member
313 Points
94 Posts
Re: HttpClient Issues
Feb 25, 2012 12:28 AM|LINK
Yep. This is what makes the header api distinction even more confusing. All the headers should be cohesive not decoupled into two buckets.
Just some feedback. Thanks for responding.
Dave
@dbettin
Darrel Mille...
Member
176 Points
56 Posts
Re: HttpClient Issues
Feb 25, 2012 01:30 AM|LINK
The latest HTTP spec explictly calls out Payload Headers (3.1) and Representation Header Fields (4.1) in the payload spec http://tools.ietf.org/html/draft-ietf-httpbis-p3-payload-18 and the request and response headers are documented in the semantics spec http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-18.
I think adding the distinction will make the purpose of the headers much clearer in the long run.
I've seen too many people try and use content-type as a way of requesting a particular representation format.