Difference between SocketsHttpHandler and HttpClientHandler in iOS Application NET7

ヴィン- vinh-nn2 1 Reputation point
2023-10-24T04:37:01.6666667+00:00

I am using SocketsHttpHandler and HttpClientHandler as parameters when making a request with HttpClient in iOS Application NET7

Try creating a simple "Hello World" application with the code below:

            try
            {
                var dataContent = new StringContent("", Encoding.UTF8, "text/xml");
                SocketsHttpHandler socket = new SocketsHttpHandler();
                NetworkCredential userCredentials = new NetworkCredential("userAdmin@domain.local", "Password");
                CredentialCache credentialsCache = new CredentialCache();
                Uri uri = new Uri("URI Exchange Server");
                credentialsCache.Add(uri, "NTLM", userCredentials);
                socket.Credentials = credentialsCache;
                HttpClient http = new HttpClient(socket);
                http.Timeout = TimeSpan.FromMilliseconds(20000);
                HttpResponseMessage res = await http.PostAsync("URI Exchange Server", dataContent);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            

Currently running on IOS devices with version 15/16

Strange phenomenon (1):

If using SocketsHttpHandler as a parameter for HttpClient, when installing the app for the first time, a dialog box requesting access to "Local Network" will appear (as shown in the image).

IMG_0157

  • If you choose "Allow": after 20 seconds timeout, an error message will be displayed (The request was canceled due to the configured HttpClient.Timeout of 20 seconds elapsing.)
  • If you choose "Don't Allow", the request will be successful (200 OK)

=> Because the username (userAdmin@domain.local) contains the suffix ".local", if it is not .local, the dialog box to confirm "Local Network" will not appear.

If using HttpClientHandler as a parameter for HttpClient, the dialog box to confirm "Local Network" will also not appear.

The result of the request is successful (200 OK), even if the username contains the suffix ".local", the request will also be successful (200 OK)

Why is there a difference above?

Strange phenomenon (2):

The username in the above example is userAdmin@domain.local (the domain name value behind @ is the correct value)

If the value is wrong, for example userAdmin@domainabc.wrong (the domain name value behind @ is the wrong value), then:

  • If using HttpClientHandler to request, the result is FAIL (authentication failed)
  • If using SocketsHttpHandler, the result is still 200 OK

Why is there a difference above?

Is there a way to set "Allow Local Network" to always be "NO"? I tried the code below in Info.plist, but the "Local Network" confirmation dialog still always appears.

    <key>NSAppTransportSecurity</key>
    <dict> 
      <key>NSAllowsLocalNetworking</key>
      <false/>
    </dict>
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes