Repository navigation
Closed
Labels
Description
Describe the bug
It is impossible to send request when using specific user agent.
To Reproduce
using RestSharp;
var options = new RestClientOptions("http://dummyjson.com/");
//options.UserAgent = "xxx"; //this works fine
options.UserAgent = "com.example.app/1.2.34(App; android 16) Build/1234"; //this does not work
var client = new RestClient(options);
var request = new RestRequest("products/1");
var response = await client.GetAsync(request);
Console.WriteLine(response.Content);
Expected behavior
You should see some json on the console.
Stack trace
System.FormatException
HResult=0x80131537
Message=The format of value 'com.example.app/1.2.34(App; android 16) Build/1234' is invalid.
Source=System.Net.Http
StackTrace:
at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index) in /_/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaderParser.cs:line 72
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value) in /_/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs:line 989
at RestSharp.RestClient.ConfigureHttpClient(HttpClient httpClient) in /home/runner/work/RestSharp/RestSharp/src/RestSharp/RestClient.cs:line 133
at RestSharp.RestClient..ctor(RestClientOptions options, Action`1 configureDefaultHeaders) in /home/runner/work/RestSharp/RestSharp/src/RestSharp/RestClient.cs:line 64
at Program.<<Main>$>d__0.MoveNext() in C:\Users\user\source\repos\F\F\Program.cs:line 8
at Program.<Main>(String[] args)
Desktop (please complete the following information):
- OS: Windows 10
- .NET 6
- Version 108.0.2
Additional context
I was trying to replicate behaviour of one android application using RestSharp.
I need to use that specific user agent, otherwise server does not reply.
I didn't find any limitations on documentation page regarding user agent, so I suppose it should work.
Last place in Restsharp is in this line, then parsing fails .NET code:
RestSharp/src/RestSharp/RestClient.cs
Line 129 in 79848da
| httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Options.UserAgent); |
Activity
alexeyzimarev commentedon Oct 17, 2022
As you see from the stack trace, the exception is not from RestSharp. It is thrown by the .NET, as you already found out. How exactly do you propose to fix it here? I could only suggest opening an issue in the .NET runtime repository, not here.
alekw commentedon Oct 17, 2022
The fix would be basically to replace this line:
RestSharp/src/RestSharp/RestClient.cs
Line 129 in 79848da
with that one:
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Options.UserAgent);Maybe some additional flag could be introduced to enable usage of malformed user agents, as they frequently occur in the wild.
Then one of these lines would be used depending on the flag.
alexeyzimarev commentedon Nov 8, 2022
I don't think that we should care about the user agent format that much, so if you submit a PR that would add it unparsed, I will merge it.