Answer the question
In order to leave comments, you need to log in
How to make a request through a specific network device in .net?
There are two active network devices on the computer:
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;
}
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question