Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The version in HttpRequestMessage had no validate #56662

Closed
WeihanLi opened this issue Jul 31, 2021 · 6 comments
Closed

The version in HttpRequestMessage had no validate #56662

WeihanLi opened this issue Jul 31, 2021 · 6 comments

Comments

@WeihanLi
Copy link
Contributor

When I try to specify the HTTP version for the HTTP request, I found that there's no validation on the version, you can set whatever version you want, the request message ToString() would output the version directly

For example:

var request = new HttpRequestMessage(HttpMethod.Get, "https://google.com")
{
    Version = new Version(4, 0)
};
Console.WriteLine(request.ToString());

output:

Method: GET, RequestUri: 'https://google.com/', Version: 4.0, Content: <null>, Headers:
{
}

while the HTTP version used is HTTP/1.1, not the 4.0
image

Maybe the request version would be misleading if the HTTP version in HttpRequestMessage is not the same with the actual
HTTP version

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Jul 31, 2021
@ghost
Copy link

ghost commented Jul 31, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

When I try to specify the HTTP version for the HTTP request, I found that there's no validation on the version, you can set whatever version you want, the request message ToString() would output the version directly

For example:

var request = new HttpRequestMessage(HttpMethod.Get, "https://google.com")
{
    Version = new Version(4, 0)
};
Console.WriteLine(request.ToString());

output:

Method: GET, RequestUri: 'https://google.com/', Version: 4.0, Content: <null>, Headers:
{
}

while the HTTP version used is HTTP/1.1, not the 4.0
image

Maybe the request version would be misleading if the HTTP version in HttpRequestMessage is not the same with the actual
HTTP version

Author: WeihanLi
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@MihaZupan
Copy link
Member

MihaZupan commented Jul 31, 2021

The default HttpVersionPolicy is RequestVersionOrLower, so establishing 1.1 here is fine.

If you instead set it to Exact or RequestVersionOrHigher, you should see an exception being thrown before anything is sent.

If you want to see the actual version that was established, you have to look at the HttpResponseMessage

@MihaZupan MihaZupan removed the untriaged New issue has not been triaged by the area owner label Jul 31, 2021
@karelz
Copy link
Member

karelz commented Jul 31, 2021

If you want to avoid typos, you can use pre-defined versions: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpversion

Closing as By Design.

@karelz karelz closed this as completed Jul 31, 2021
@karelz karelz added this to the 6.0.0 milestone Jul 31, 2021
@WeihanLi
Copy link
Contributor Author

The request message version is the same as the request message version in the response message, while the HTTP version in the response message is 1.1, and it seemed it would not throw an exception even when I set the DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact for httpClient

image

When I set DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact for the request message, I would get an exception

image

Thanks for your quick answer @MihaZupan @Kahbazi

@MihaZupan
Copy link
Member

That's correct, the version policy set on HttpClient applies to helper methods that take Uri/string.
If you provide an HttpRequestMessage yourself, its policy applies.

@WeihanLi
Copy link
Contributor Author

Got it, great thanks @MihaZupan

@ghost ghost locked as resolved and limited conversation to collaborators Aug 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants