C# Only http and https schemes are allowed - HttpClient

Markus Freitag 3,791 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?

Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.