有没有办法指定哪个NetworkInterface .net将使用?

时间:2018-03-30 18:19:59

标签: c# .net httpclient network-interface

我想使用其Wifi和LAN连接在设备上运行一系列测试。我有两个网络接口卡,一个是WiFi,另一个是有线的。该设备具有可通过两个网络接口访问的单个IP地址。有没有办法保证我的机器使用特定的网络接口,以便我可以通过有线连接运行所有测试,然后通过Wifi连接运行所有测试?

请记住,两个连接共享同一个子网,因此要么成功连接到设备。我只是希望能够来回切换并指定我在任何给定时间使用的连接。

我在.net上使用HttpClient和C#。有没有办法强制HttpClient使用特定的网络接口?它似乎根据IP地址自动选择一个,但在这种情况下,有2个网络接口可以工作。我需要能够以编程方式来回切换以运行我的自动化测试。

我注意到了一种称为“接口度量”的东西,可以通过IP4属性高级TCP / IP设置手动更改。每次都会使用编号较小的接口吗?有没有办法以编程方式更改此值?

更多澄清:我没有直接跳转到禁用WiFi NIC或有线网卡来回的原因是当您禁用WiFi网卡时,断开连接的设备并且它不会自动重新连接你启用它。 (即使您说'自动连接',重新启用时也不会连接)。但是,我找到了一个快速解决方案。将有线NIC上的Interface Metric设置为低于WiFi NIC上的Interface Metric。然后,只需启用和禁用有线NIC。禁用时,系统将使用WiFi NIC。启用后,系统将使用有线NIC,因为其接口度量标准设置得较低。现在,我只需要弄清楚如何以编程方式启用/禁用有线网卡...

4 个答案:

答案 0 :(得分:2)

您可以使用库 WlanAPI.cs

static public void Connect_to_wifi(string ssid)
            {
                WlanClient client = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    // Lists all networks with WEP security
                    //Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                    //foreach (Wlan.WlanAvailableNetwork network in networks)
                    //{
                    //    if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP)
                    //    {
                    //        Console.WriteLine("Found WEP network with SSID {0}.", network.profileName);
                    //    }
                    //}
    
                    // Retrieves XML configurations of existing profiles.
                    // This can assist you in constructing your own XML configuration
                    // (that is, it will give you an example to follow).
                    foreach (Wlan.WlanProfileInfo profileInfo in wlanIface.GetProfiles())
                    {
                        if (profileInfo.profileName == ssid)
                        {
                            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, wlanIface.GetProfileXml(profileInfo.profileName), true);
                            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileInfo.profileName);
                            break;
                        }
                    }
                }
            }

答案 1 :(得分:1)

一种方法是更新路由表以强制特定IP到特定NIC的流量。路由表用于决定发送流量的NIC。

你可以通过以下两种方式实现这一目标:

1)使用P / Invoke用C#代码更新路由表(参见here for a good example,但基本上你正在调用CreateIpForwardEntry)。

2)使用Process.Start来调用route.exe,例如

route.exe ADD destination_network MASK subnet_mask  gateway_ip metric_cost

route.exe CHANGE destination_network MASK subnet_mask  gateway_ip metric_cost

答案 2 :(得分:1)

最好的方法是使用WinSock绑定:TCP/IP connection on a specific interface

但是,如果您拥有bith NIC的单个IP,那么:

从PowerShell中禁用NIC:https://docs.microsoft.com/en-us/powershell/module/netadapter/disable-netadapter?view=win10-ps

从c#调用PowerShell:https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

答案 3 :(得分:0)

我最终通过将有线NIC上的Interface Metric设置为低于Wifi NIC上的Interface Metric来解决此问题。通过这种方式,我能够简单地禁用并启用有线NIC,这将有效地在测试期间随时更改正在使用的NIC。要禁用NIC,我使用netsh.exe命令和以下代码。 “命令”是“启用”或“禁用”,具体取决于我是打开还是关闭它:

        System.Diagnostics.Process p = new System.Diagnostics.Process
        {
            StartInfo =
            {
                FileName = "netsh.exe",
                Arguments = $"interface set interface \"{nicName}\" {command}",
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };

        bool started = p.Start();
相关问题