C# Only http and https schemes are allowed - HttpClient

Markus Freitag 3,786 Reputation points


I have a problem.

using (HttpClient client = new HttpClient())

 client.BaseAddress = new Uri(CFG.Baseaddress); 
 client.Timeout = new TimeSpan(0, 0, CFG.Timeout);

 string authorization = $"{CFG.Username}:{CFG.Password}";

 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(authorization)));
 urlParameters = CFG.UrlGetOrderData.Replace("{orderId}", orderId);
 response = client.GetAsync(urlParameters).Result;     ///######

// My request

/// #####Exception Only http and https schemes are allowed Parametername RequestUri

Why? What can the solution look like? What do I have to do?

The server can be different after all?
When I call the address in Edge it works.
Not with the code! Why?

A set of technologies in the .NET Framework for building web applications and XML web services.
3,299 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,350 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,161 Reputation points

    The error indicates the base address does not start with http or https. Please run your code through the debugger to find the value of CFG.Baseaddress. Unfortunately, you did not share this part of the code.

    Keep in mind, you are not implementing HttpClient according to the openly published documentation.

    HttpClient Class

    Use async/await according to he openly published documentation rather than Result which blocks the main thread.

    Asynchronous programming with async and await

    Use string interpolation rather than replace.

    $ - string interpolation (C# reference)

0 additional answers

Sort by: Most helpful