Skip to content

FormatException: Useragent is invalid #1943

@alekw

Description

@alekw

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:

httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Options.UserAgent);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions