0

I have did this several times on Visual Studio C# on Windows.

Now I am using my Mac - creating a Xamarin App in Visual Studio C# and this simple code does not work:

using (HttpClient myClient = new())
            {
                myClient.DefaultRequestHeaders.Accept.Clear();
                myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await myClient.GetAsync(url);
                string result = await response.Content.ReadAsStringAsync();
                return result;
}

The url is: "http://webcode.me".

I tried to create a URI, same issue.

What happens is, that when the code line:

HttpResponseMessage response = await myClient.GetAsync(url);

is called, it never returns. And the app never starts.

Of course, the url I am using is just a test url, but after calling this method, my output window looks like this:

    2021-04-16 15:40:07.653142+0200 PHC.iOS[3243:87620] Starting Up
Resolved pending breakpoint at 'Main.cs:35,1' to void PHC.iOS.Application.<GetBridges>d__2.MoveNext () [0x000b2].
Resolved pending breakpoint at 'Communicator.cs:44,1' to void PHC.BridgeCommunication.Communicator.<DoGet>d__4.MoveNext () [0x00189].
2021-04-16 15:40:07.664131+0200 PHC.iOS[3243:87620] Now trying URL: http://webcode.me
Resolved pending breakpoint at 'Communicator.cs:58,1' to void PHC.BridgeCommunication.Communicator.<DoGet>d__4.MoveNext () [0x001b9].
Thread started:  #2
2021-04-16 15:40:08.249780+0200 PHC.iOS[3243:87661] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=3243
2021-04-16 15:40:08.250077+0200 PHC.iOS[3243:87661] SecTaskCopyDebugDescription: PHC.iOS[3243]/0#-1 LF=0
2021-04-16 15:40:08.259552+0200 PHC.iOS[3243:87661] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
Thread started:  #3
Thread started:  #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6

And it continues forever with the Thread starting and Thread stopping information.

The "Starting Up" and "Now trying URL: http://webcode.me" are console output from the class calling this function. After writing these things in the console, the method shown above is called.

I can get things to work on my Windows Visual Studio, with no problems at all.

I am NOT Mac-expert, but I am wondering if my Mac is somehow blocking the call. The firewall is turned off.

Can anyone help me a bit?

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
zorci
  • 145
  • 1
  • 9
  • Google `SecTaskLoadEntitlements failed error=22 cs_flags=200`, for example: https://apple.stackexchange.com/questions/237465/what-means-sectaskloadentlitlements-failed-error-22 – Andy Apr 19 '21 at 04:43
  • For the implementation of HttpClient, you could check the links below. Android: https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=macos iOS: https://learn.microsoft.com/en-us/xamarin/cross-platform/macios/http-stack – Wendy Zang - MSFT Apr 20 '21 at 06:46
  • Thank you both for your input. I followed the links and read all about this. :) Now, I have not been honost with you, the URL I am trying to reach is "https://....". So it is encrypted. The URL above was a test URL I tried to see if that worked. So my issue is not regarding no-SSL. I tried the options found in your links anyway - and still get the same error. – zorci Apr 22 '21 at 06:31
  • Have you try to use other link? Still get the same error? – Wendy Zang - MSFT Apr 23 '21 at 08:51
  • Yes, tried 3 different URL’s. – zorci Apr 25 '21 at 07:31
  • As i know, it would be work. Please check the link. https://stackoverflow.com/questions/50950959/does-the-managed-xamarin-httpclient-implementation-support-tls-1-2 – Wendy Zang - MSFT Apr 26 '21 at 06:41
  • I just created a new console-project. Still on my Mac. And in Main() I just called the URL async. It worked. So, it seems to be my project that somehow won't work. I am very confused. But thanks Wendy Zang. I will look at the link now. – zorci Apr 26 '21 at 07:17
  • The Console project worked right away, as mentioned above. I then tried to create a new Multiplatform project. A "Blank Forms App". And I copy pasted the exact same code as in the Console application. Pasted it into Main.cs in the iOS project in the solution. Nothing else. And again, I got the error. So it has to be something regarding the type of project I think. I also read the link you gave me. I have tried all 3 different kind of HttpClient implementations. Still a mystery. – zorci Apr 26 '21 at 08:05
  • This the code that works in Main() in my newly created Console application. But NOT working in my newly created Multiplatform "Blank forms app" in the Main.cs-file Main() function in the .iOS project inside the solution. – zorci Apr 26 '21 at 14:03
  • Console.WriteLine("Start"); HttpClient client = new HttpClient(); var responses = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1"); string results = await responses.Content.ReadAsStringAsync(); Console.WriteLine(results); Console.WriteLine("End"); – zorci Apr 26 '21 at 14:03
  • Check the link below for ios. https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http Is there any details of the error? – Wendy Zang - MSFT Apr 30 '21 at 09:31

1 Answers1

0

Okay - first of all. The error had nothing to do with the API-call. The API-call was made async, and the application continued and ran into that error.

I removed the API-call and figured that out.

Then I changed from using HttpClient to use HttpWebRequest and suddenly my requests went through and returned nicely.

I still don't know why my HttpClient did not work at all, but I will close this ticket as I am not in need for answers anymore. Thank you for your comment everyone.

zorci
  • 145
  • 1
  • 9