O
O
Ogoun Er2020-05-23 17:52:48
Computer networks
Ogoun Er, 2020-05-23 17:52:48

How to make a request through a specific network device in .net?

There are two active network devices on the computer:
5ec9370511b6d191567859.jpeg
One is through the built-in network card via wire, the second is USB WiFi.

To work with the network, the built-in network card is used by default.
But for part of the requests from the .net application, I want to use a wifi network.
How can I do that?

I tried looking up the network interface address like this:

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                if (adapter.Description.Contains("TP-LINK"))//TP-LINK
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    foreach (var address in properties.UnicastAddresses)
                    {
                        if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            Log.Info($"Use interface {adapter.Name}. IP: {address.Address}");
                            _useIp = true;
                            _address = address.Address;
                            break;
                        }
                    }
                }
                if (_useIp) break;
            }


And then when creating a query, do it explicitly
private static IPEndPoint BindIP(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
            if (retryCount > 3) return null;
            return new IPEndPoint(_address, 0);
 }

var request = WebRequest.CreateHttp(url);
            if (_useIp)
            {
                request.ServicePoint.BindIPEndPointDelegate = BindIP;
            }


But this approach doesn't work. Moreover, if I try to put the address of the wired network, then it generally goes into an endless loop if you do not exit, as in the example, when the number of attempts is exceeded.

I make requests via https. Maybe there are other options?

PS: Windows 10 operating system

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2020-05-23
@d-stream

Why try to turn the network stack fur inside out?
Isn't it easier to just set routes using regular means?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question