C# Only http and https schemes are allowed - HttpClient

Markus Freitag 3,786 Reputation points
2022-03-22T08:56:13.103+00:00

Hello,

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
https://192.165.22.11:5000/api/Scada/ProductionOrderNonSerializedMaterialValidate?productionOrderNo=23214

/// #####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?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,268 questions
C#
C#
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,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2022-03-22T10:35:28.157+00:00

    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