C# FtpWebRequest在上次成功连接后使用正确的密码忽略错误的密码
我在使用C#中的C# FtpWebRequest在上次成功连接后使用正确的密码忽略错误的密码,c#,.net,ftp,webrequest,ftpwebrequest,C#,.net,Ftp,Webrequest,Ftpwebrequest,我在使用C#中的FtpWebRequest类时遇到了一个问题 当我第一次尝试使用正确的凭据将文件上载到FTP服务器,第二次尝试使用错误的凭据(用户名相同但密码错误)时,不会引发异常,并且文件仍然上载到FTP服务器。请考虑以下代码: using System; using System.Net; internal class Program { private static void Main(string[] args) { var uri = new Uri(
FtpWebRequest
类时遇到了一个问题
当我第一次尝试使用正确的凭据将文件上载到FTP服务器,第二次尝试使用错误的凭据(用户名相同但密码错误)时,不会引发异常,并且文件仍然上载到FTP服务器。请考虑以下代码:
using System;
using System.Net;
internal class Program
{
private static void Main(string[] args)
{
var uri = new Uri("ftp://ftp.dlptest.com/TestFile.txt");
var method = WebRequestMethods.Ftp.UploadFile;
//Here I'm uploading the test file using correct credentials
var correctCredentials =
new NetworkCredential("dlpuser@dlptest.com", "fwRhzAnR1vgig8s");
DoFtpRequest(uri, correctCredentials, method);
//Here I'm uploading the test file using wrong credentials.
//I expect some exception to be thrown and the file not being
//uploaded to the server, neither is the case.
var wrongCredentials =
new NetworkCredential("dlpuser@dlptest.com", "WRONG_PASWORD");
DoFtpRequest(uri, wrongCredentials, method);
}
public static FtpWebResponse DoFtpRequest(
Uri uri, NetworkCredential credentials, string method)
{
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
return (FtpWebResponse)request.GetResponse();
}
}
这里我使用的是一个公共ftp服务器ftp://ftp.dlptest.com/
我在这里找到的,可以用来测试此代码
正如您所看到的,首先我尝试上载具有正确凭据的文件,然后上载具有错误凭据的文件(使用相同的用户名但更改密码)。
但该文件仍会上载到服务器。如果我首先尝试使用错误的凭据,则会引发异常,并且一切都按预期工作
你知道发生了什么事吗?这是框架的一个缺陷吗?我有什么办法来处理这个问题,因为它会导致我现在正在使用的程序出现问题?
FtpWebRequest
在引擎盖下使用连接池。看
连接池的键只有主机名、端口号、用户名和可选的连接组名
第二个请求重用第一个请求的连接,并且从不使用错误的密码。这是因为这两个请求使用相同的连接池,因为它们的不同之处只是密码,而密码不是密钥的一部分 但是,如果交换请求,第一个请求将不会成功,其连接将关闭,并且无法连接到池。第二个请求必须从一个新连接开始,它将使用正确的密码
要隔离请求,您可以:
- 对不同的请求使用唯一的连接池,使它们使用不同的连接池
- 或禁用以完全禁用连接池