C
C#11mo ago
jgomezal

✅ An API refuses my Connection from MAC but not Windows.

Hello there!! Could you give me a hand with this? I'm using MAUI & Blazor. Previously, I didn't get any issues connecting to the API, however, after updating the VS I'm not able to establish the connection with the API URL. .Net 7 VS 17.6.3 (build 421) for MAC These are some points for bearing in mind: * The API URL doesn't use a secure connection HTTPS but HTTP. * I work with the same code from a Windows and a MAC machine. It works fine from Windows and Postman. The connection is not rejected. * It is not because of the credentials, I use the same from Windows and Postman and everything works fine. This is the method I'm using for establishing the connection: private JObject _requestPost(Uri endpoint, Dictionary<string, Array> objSend) { try { using var webclient = new HttpClient(); var newPostJson = JsonConvert.SerializeObject(objSend); var payload = new StringContent(newPostJson, System.Text.Encoding.UTF8, "application/json"); Task<HttpResponseMessage> httpResponse = webclient.PostAsync(endpoint, payload); //Logger.info("Connection: request payload = " + newPostJson); HttpResponseMessage httpResponseMessage = httpResponse.Result; Logger.info("Connection: response received from " + endpoint + " with status code: " + httpResponseMessage.StatusCode); return (JObject)JsonConvert.DeserializeObject(httpResponseMessage.Content.ReadAsStringAsync().Result)!; } catch (Exception e) { Logger.error("Connection: " + e.Message); return (JObject)JsonConvert.DeserializeObject("{ }")!; } } My Log message with the catch(Exception e):
Connection: One or more errors occurred. (The resource could not be loaded because the App Transport Security policy requires the use of a secure connection)
The attached picture is the one when debugging the connection step. Thanks in advance. Juan
11 Replies
Pobiega
Pobiega11mo ago
Read the error message again. It's pretty clear in what it says There is an "app transport security policy" that requires a secure connection (https). You need to figure out a way to change that policy.
jgomezal
jgomezal11mo ago
Hello @Pobiega yes I read the message, that's why I tried using an HTTPS connection but the API doesn't have it. The strange point is that I'm receiving the error only from Mac. I'm not receiving the error from Windows even from Postman. This is why I decided to ask for help here.
Pobiega
Pobiega11mo ago
So the problem is in your OS on the mac google "mac transport security policy" and see what you find
jgomezal
jgomezal11mo ago
Thank you so much! It helped me. I had to modify the Info.plist on my project to allow the unsecured connection to the HTTP, but only for a specific domain.
Florian Voß
Florian Voß11mo ago
something unrelated - don't call your HttpClient instance webclient, its not a WebClient
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
jgomezal
jgomezal11mo ago
Thank you for this recommendation @Florian Voß Well, you are right. I need to check some of your points for learning because this is my first time with C#. The app works but I know I need to improve a lot my code and how I’m consuming the resources. I will review carefully your comments and see what else I can do. Thank you so much!!
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
jgomezal
jgomezal11mo ago
Manu thanks @TeBeClone ... I didn't know about the DTO concept. I already started with some modifications for using the await and async. I need to update some other sections before starting updating the DTO recommendations. I usually worked with arrays directly in PHP. I'm currently learning how to use better in both languages (C# and PHP) these definitions for Classes instead of Dictionaries and Arrays. To be honest, sometimes it is a bit complicated for me. This API (I mean, this post), requires a body with more than 2 hierarchy levels, so that's why I'm using a Dictionary, and an array, for finally building the JObject. But I will try to use this DTO for improving and learning.
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts