question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

C# Only http and https schemes are allowed - HttpClient

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?

dotnet-csharpdotnet-aspnet-generaldotnet-aspnet-webformsdotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered MarkusFreitag-0088 commented

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)


· 16
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

     https://192.165.22.11:5000/    // BaseAddress
    
    
     api/Scada/ProductionOrderNonSerializedMaterialValidate?productionOrderNo={orderId} // urlParameters 



I have two customers, with one it works, with the other not. What exactly should I change? Or is it the customer?






0 Votes 0 ·

If it works with one and not the other perhaps check windows event and IIS logs.

1 Vote 1 ·
AgaveJoe avatar image AgaveJoe MarkusFreitag-0088 ·

I'm a little confused by your response, 192.165.22.11 is an internal address. Based on the carelessness of your original code, l'm guessing 192.165.22.11 is the development environment base address not the second customer's base address.

Again, the error indicates the URL is missing http or https.

0 Votes 0 ·

The original error message.


Do you have any tip for me? It is https and it works for customer 1 well, but not for customer 2.

Thanks in advance. What can be the cause, what can I do? What can I check?

0 Votes 0 ·
Show more comments