alanmcgovern / Mono.Nat

UPNP and NAT-PMP port forwarding for .NET
https://github.com/mono/Mono.Nat
MIT License
160 stars 156 forks source link

Replace HttpWebRequest with HttpClient #18

Closed Bond-009 closed 3 years ago

Bond-009 commented 3 years ago

HttpWebRequest should not be used anymore https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=netstandard-2.0#remarks

alanmcgovern commented 3 years ago

Unfortunately HttpClient doesn't allow specifying which network interface a given request should be sent on. If you have multiple active network interfaces on your device, Mono.Nat will be unreliable if it can't respond on the same NIC the uPnP/NAT-PMP device is on.

The upnp discovery code iterates over all active NICs and sends a discovery message on each NIC. UPnPNatDevice then binds the web request to the right local ipaddress/NIC using the BindIPEndPointDelegate delegate.

This capability might be supported again with .NET 5 ( https://github.com/dotnet/runtime/issues/1793 ) but I wouldn't bet on it. It's been a multi-year ask and it's only just been approved as an API. Even if it is supported at this point, it won't be usable in any existing .NET standard profiles.

I expect that HttpWebRequest will be supported for the foreseeable future as there is no alternative to it :)

I'll go ahead and close this, but if you have any ideas please feel free to reopen this!)