C#FtpWebRequest集合接口

C#FtpWebRequest集合接口,c#,ftp,C#,Ftp,我的计算机上有许多网络接口(包括WiFi、以太网)。 如果要将特定网络接口设置为使用FtpWebRequest, 可能吗?或者Windows会选择正确的界面? 这是我的密码 // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + targetIP + ":212

我的计算机上有许多网络接口(包括WiFi、以太网)。 如果要将特定网络接口设置为使用
FtpWebRequest
, 可能吗?或者Windows会选择正确的界面? 这是我的密码

            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + targetIP + ":2121/project.zip");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.UseBinary = true;
            request.UsePassive = true;
            request.Timeout = 20 * 1000;
            request.ReadWriteTimeout = 20 * 1000;


            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("test", "test");

            using (var inputStream = File.OpenRead(p))
            using (var outputStream = request.GetRequestStream())
            {
                var buffer = new byte[1024 * 1024];
                int totalReadBytesCount = 0;
                int readBytesCount;
                while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputStream.Write(buffer, 0, readBytesCount);
                    totalReadBytesCount += readBytesCount;
                    Transfer.transfer[0].transferLength += totalReadBytesCount;

                    double progress = (double)Transfer.transfer[0].transferLength / Transfer.transfer[0].totalLength;
                    e.Window.Message = CommonResource.Exporting_String + " " + Math.Round(progress * 100) + "%";
                }
            }

我认为可以通过设置ServicePoint.BindIPEndPointDelegate属性来实现

检查这个


我认为可以通过设置ServicePoint.BindIPEndPointDelegate属性来实现

检查这个


这可以通过使用
服务点。BindIPEndPointDelegate

 public static void ExecuteRequest()
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
        request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }

    private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        // We want to make sure we try everything for the request to pass.
        IPEndPoint defaultEndPoint = null;
        foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            // Loop through all interfaces, check if the current interface is 'Up' (ready to transmit) first.
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                bool returnIP = false;
                //if (networkInterface.Id == "MyAdapter")
                //if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // If the interface matches our wishes, tell the next block of code to return the (if) found matching IP
                    returnIP = true;
                }

                if (returnIP || defaultEndPoint == null)
                {
                    // Loop through the UnicastAddresses assigned to this interface.
                    foreach (UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
                    {
                        // Check if any of the addresses is IPv4
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            // If so, create ourselves a new IPEndPoint and if needed return it.
                            defaultEndPoint = new IPEndPoint(ip.Address, 0);
                            if (returnIP)
                            {
                                return defaultEndPoint;
                            }
                        }
                    }
                }
            }
        }
        if (defaultEndPoint == null)
        {
            // Optional:
            //throw new NotSupportedException("A valid internet connection is required for this program to run.");
        }
        return defaultEndPoint;    
    }

我已经将其用于webrequests(如图所示),但对于
FTPWebRequest
它也会起作用。请注意,回调方法将始终尝试返回适配器,即使您正在查找的适配器不可用。

这可以通过使用
服务点来实现。BindIPEndPointDelegate

 public static void ExecuteRequest()
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
        request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }

    private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        // We want to make sure we try everything for the request to pass.
        IPEndPoint defaultEndPoint = null;
        foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            // Loop through all interfaces, check if the current interface is 'Up' (ready to transmit) first.
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                bool returnIP = false;
                //if (networkInterface.Id == "MyAdapter")
                //if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // If the interface matches our wishes, tell the next block of code to return the (if) found matching IP
                    returnIP = true;
                }

                if (returnIP || defaultEndPoint == null)
                {
                    // Loop through the UnicastAddresses assigned to this interface.
                    foreach (UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
                    {
                        // Check if any of the addresses is IPv4
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            // If so, create ourselves a new IPEndPoint and if needed return it.
                            defaultEndPoint = new IPEndPoint(ip.Address, 0);
                            if (returnIP)
                            {
                                return defaultEndPoint;
                            }
                        }
                    }
                }
            }
        }
        if (defaultEndPoint == null)
        {
            // Optional:
            //throw new NotSupportedException("A valid internet connection is required for this program to run.");
        }
        return defaultEndPoint;    
    }

我已经将其用于webrequests(如图所示),但对于
FTPWebRequest
它也会起作用。请注意,回调方法将始终尝试返回适配器,即使您正在查找的适配器不可用。

Wifi和以太网是7层模型的低层(物理)协议,而ftp是更高层(应用程序)的协议。ftp可以在这两个服务器上运行,这取决于您连接的服务器。请在此阅读更多()谢谢!我已经阅读了OSI模型。这种情况可能是WiFi和以太网都连接在不同的LAN中,并且它们有自己的IP地址。因此,Windows将自动确定使用哪种网络接口?Wifi和以太网是7层模型的低层(物理)协议,而ftp是更高层(应用程序)的协议。ftp可以在这两个服务器上运行,这取决于您连接的服务器。请在此阅读更多()谢谢!我已经阅读了OSI模型。这种情况可能是WiFi和以太网都连接在不同的LAN中,并且它们有自己的IP地址。那么,Windows将自动决定使用哪个网络接口?