C# FtpWebRequest set interface
本文关键字:interface set FtpWebRequest | 更新日期: 2023-09-27 18:36:29
我的计算机上有许多网络接口(包括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
:
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
它也可以工作。请注意,回调方法将始终尝试返回适配器,即使您要查找的适配器不可用也是如此。
我认为可以通过设置 ServicePoint.BindIPEndPointDelegate 属性来实现
检查这个http://msdn.microsoft.com/library/ms144146%28en-us,vs.80%29.aspx
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + targetIP + ":2121/project.zip");
request.ServicePoint.BindIPEndPointDelegate = delegate
{
return new IPEndPoint(IP, PORT);
};