1
\$\begingroup\$

Before starting, I know I can use the UnityWebRequest class instead. The problem is that it is another library the one that is failing: NEthereum's source code, which makes heavy use of the HttpClient.

So I tried this in a behaviour (2021.3.25):

using System;
using System.Net.Http;
using UnityEngine;

public class SampleBehaviour : MonoBehaviour
{
    public void Start()
    {
        TestHttp();
    }

    private async void TestHttp()
    {
        using var client = new HttpClient();
        client.BaseAddress = new Uri("http://127.0.0.1:8545");
        await client.GetAsync("/").ConfigureAwait(false);
    }
}

(I tried other local addresses, like my LAN ones, considering that http server is attending on 0.0.0.0 and I succeed hitting it with cURL)

BUT when I try this, I get this exception:

SocketException: An address incompatible with the requested protocol was used

Now, the issue is that I use IPv4 in a daily basis in other places. What's wrong? Is it something in the .NET setup in the current Unity version? (I'm using .NET Framework and, again, 2021.3.25)

More context here:

I made this test in Unity:

the new code

And still get the error:

same address error

While a stand-alone .NET Framework console application does not fail like this:

standalone app

And, more important: This HttpClient-related things were working before. This happened after moving to a newer version of Unity (the 2021.3.25 I'm using now). This is why I'm posting the question here and not in a pure C#-related site.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ This is a general question about .NET, not about game development, and doesn't belong here. As for a possible solution, see stackoverflow.com/questions/23438416/… \$\endgroup\$
    – Kevin
    Aug 18, 2023 at 0:52
  • \$\begingroup\$ Dude, most likely if I state this in stackoverflow they'll lead me here because this is, in particular, a matter of the .NET platform that is embedded in the Unity version in particular. \$\endgroup\$ Aug 18, 2023 at 2:12
  • \$\begingroup\$ I only have right now a 2021.1.15f version, but the code you posted results in NotImplementedException for the SslProtocols. Removing the constructor and not assigning the protocol at all, I'm getting a response when you assign the await as response and read it like var contents = await response.Content.ReadAsStringAsync(); \$\endgroup\$
    – Zibelas
    Aug 18, 2023 at 7:33
  • 1
    \$\begingroup\$ Your code works for me as-is in Unity 2021.3.29f1, so maybe you should update from your very outdated version to the latest version. But as per the article I linked, it's best for the slash to be at the end of the base address (or at least that used to be the case). \$\endgroup\$
    – Kevin
    Aug 18, 2023 at 17:26
  • 3
    \$\begingroup\$ Note that you don't need to separate your edits with headings and dates — edit history is already tracked automatically for anyone who's curious about what was added when. Best practice is to edit the question so it reads as though all relevant information was present from the very start, rather than as a chain of multiple staged updates. This helps readers get an accurate understanding of your issue quickly. \$\endgroup\$
    – DMGregory
    Aug 18, 2023 at 18:37

1 Answer 1

2
\$\begingroup\$

You specifically say that you want to use SSL/TLS encryption (SslProtocols = SslProtocols.Tls12,), but the protocol-part of your address is http://, not https://. Try this:

client.BaseAddress = new Uri("https://127.0.0.1:8545");

Also note that you might need a self-signed SSL certificate for the local webserver in order for https to work properly.

\$\endgroup\$
1
  • \$\begingroup\$ Added more details. Forget about the SSL thing. \$\endgroup\$ Aug 18, 2023 at 17:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .