Skip to main content
 首页 » 编程设计

C# FtpWebRequest 设置接口(interface)

2024年02月01日28bluestorm

我的电脑上有很多网络接口(interface)(包括 WiFi、以太网)。 如果我想设置一个特定的网络接口(interface)来使用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 实现:

 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;     
    } 

我已经将它用于网络请求(如图所示),但对于 FTPWebRequest 它也可以工作。请注意,回调方法将始终尝试返回适配器,即使您要查找的适配器不可用也是如此。