C# 网卡IP(网上资料整理)

//设置对外访问所使用网卡的IP

string sendingIp = "192.168.0.1";

//设置对外访问所使用的端口

int sendingPort = 5000;

Uri uri = new Uri("http://google.com");

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri);

//获取对外访问的包,并指定使用那个IP(网卡)与端口来发送数据包

ServicePoint sp = ServicePointManager.FindServicePoint(uri);

sp.BindIPEndPointDelegate =

    (servicePoint,remoteEp,retryCount) =>

         {

             return new IPEndPoint(IPAddress.Parse(sendingIp),sendingPort);

         };

//提交请求并获取返回结果

var data = new StreamReader(wr.GetResponse().GetResponseStream()).ReadToEnd();
//获取多网卡Ip
NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties(); UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses; foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) { if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) { MessageBox.Show(UnicastIPAddressInformation.Address.ToString()); } } }

 

posted on 2013-12-17 14:54  无觉-李敏  阅读(355)  评论(0编辑  收藏  举报