当服务器存在多个与公网访问的网卡(对应不同的公网IP地址)时,如何使用指定的网卡进行HTTP请求

官方文档:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.servicepoint.bindipendpointdelegate

参考来源:https://stackoverflow.com/questions/5860893/sending-httpwebrequest-through-a-specific-network-adapter

     http://www.veryhuo.com/a/view/24494.html

有时服务端的主动HTTP请求需要特定的IP地址,但在默认的请求配置中,使用哪个网卡IP地址是不确定的,这时需要主动设置请求所使用的网卡(IP地址),如果使用指定网卡可以按MAC地址查询网卡,再查询到网卡的IP地址,如果IP地址固定,则可以直接使用IP地址来指定。

class Program
    {
        public static string SpecificMac1 = "60EE5C1______"; // 192.168.1._
        public static string SpecificMac2 = "380025______"; // 192.168.1._
        public static string SpecificIp1 = "192.168.1._"; // 192.168.1._
        public static string SpecificIp2 = "192.168.1._"; // 192.168.1._
        public static string SpecificMac = "";
        public static string SpecificIp = "";

        // 此方法并不是每次都会调用,而是在刷新 ServicePoint 的时候会调用
        public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
        {
            foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (networkInterface.GetPhysicalAddress().ToString() == SpecificMac)
                {
                    if (networkInterface.GetIPProperties().UnicastAddresses.Any())
                    {
                        var info = networkInterface.GetIPProperties().UnicastAddresses.Last();

                        Console.WriteLine("找到的 IP 地址:" + info.Address.ToString());

                        return new IPEndPoint(info.Address, 0);
                    }
                }
            }

            Console.WriteLine("没找到的 IP 地址?");
            return null;
        }

        public static string Get(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // 查找 MAC 网卡
            //request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);

            // 指定 IP 地址
            request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint((a, b, c) => new IPEndPoint(IPAddress.Parse(SpecificIp), 0));

            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();
            var stream = response.GetResponseStream();
            if (stream == null)
            {
                return null;
            }
            using (StreamReader reader = new StreamReader(stream))
            {
                string result = reader.ReadToEnd();
                return result;
            }
        }

        static void Main(string[] args)
        {
            // 更快的刷新 ServicePoint 的 BindIPEndPoint
            ServicePointManager.MaxServicePointIdleTime = 0;

            var url = "http://192.168.1._/ip";
            while (true)
            {
                SpecificMac = SpecificMac1;
                SpecificIp = SpecificIp1;
                var result = Get(url);
                Console.WriteLine("返回内容1:" + result);
                Thread.Sleep(500);

                SpecificMac = SpecificMac2;
                SpecificIp = SpecificIp2;
                result = Get(url);
                Console.WriteLine("返回内容2:" + result);
                Thread.Sleep(500);
            }

            Console.ReadKey();
        }
    }

【扩展】在 TCP、UDP 通信中可能遇到的问题:https://blog.csdn.net/Scarlett_OHara/article/details/88556798

你可能感兴趣的:(当服务器存在多个与公网访问的网卡(对应不同的公网IP地址)时,如何使用指定的网卡进行HTTP请求)