我有一个用.NET 3.5编写的应用程序,它使用FTP从服务器上传/下载文件.该应用程序工作正常,但存在性能问题:
连接FTP服务器需要花费大量时间.FTP服务器位于不同的网络上,并具有Windows 2003 Server(IIS FTP).当多个文件排队等待上传时,从一个文件到另一个文件的更改会使用FTPWebRequest创建一个新连接,并且需要花费很多时间(大约8-10秒).
有可能重新使用连接吗?我不太确定KeepAlive属性.哪些连接保持活跃并重用.
Windows Server 2003上的IIS-FTP不支持SSL,因此任何人都可以通过WireShark等数据包嗅探器轻松查看用户名/密码.我发现如果IIS 7.0,Windows Server 2008在其新版本中支持基于FTP的SSL.
我基本上想要提高我的应用程序的上传/下载性能.任何想法将不胜感激.
**请注意3不是问题,但我希望人们对此发表评论
我正在尝试衡量Web服务的吞吐量.
为了做到这一点,我编写了一个小工具,可以连续发送请求并从多个线程中读取响应.
每个线程的内部循环的内容如下所示:
public void PerformRequest()
{
WebRequest webRequest = WebRequest.Create(_uri);
webRequest.ContentType = "application/ocsp-request";
webRequest.Method = "POST";
webRequest.Credentials = _credentials;
webRequest.ContentLength = _request.Length;
((HttpWebRequest)webRequest).KeepAlive = false;
using (Stream st = webRequest.GetRequestStream())
st.Write(_request, 0, _request.Length);
using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
using (Stream responseStream = httpWebResponse.GetResponseStream())
using (BufferedStream bufferedStream = new BufferedStream(responseStream))
using (BinaryReader reader = new BinaryReader(bufferedStream))
{
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
throw new WebException("Got response status code: " + httpWebResponse.StatusCode);
byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
httpWebResponse.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,除了似乎有限制工具.如果我使用每40个线程运行该工具的两个实例,那么我获得的吞吐量明显高于具有80个线程的一个实例.
我找到了ServicePointManager.DefaultConnectionLimit属性,我设置为10000(如果我按照Jader …
我正在尝试使用FtpWebRequest该方法从FTP下载文件的简单方法WebRequestMethods.Ftp.DownloadFile.问题是我不想显示下载的进度,因此需要知道前面的文件大小才能计算转移的百分比.但是,当我打电话GetResponse中FtpWebRequest的ContentLength成员为-1.
好的 - 所以我使用该方法预先获得文件的大小WebRequestMethods.Ftp.GetFileSize.没问题.在获得大小后我下载文件.
这就是问题出现的地方......
获得大小后我尝试重用FtpWebRequest并重置方法WebRequestMethods.Ftp.DownloadFile.这会导致System.InvalidOperationException类似"发送请求后无法执行此操作"之类的说法.(可能不是确切的表述 - 翻译自瑞典语).
我在其他地方发现,只要我将KeepAlive属性设置为true,无关紧要,连接保持活动状态.这是我不明白的......我创造的唯一对象是我的FtpWebRequest对象.如果我创建另一个,它怎么知道使用什么连接?什么凭据?
伪代码:
Create FtpWebRequest
Set Method property to GetFileSize
Set KeepAlive property to true
Set Credentials property to new NetworkCredential(...)
Get FtpWebResponse from the request
Read and store ContentLength
Run Code Online (Sandbox Code Playgroud)
现在我得到了文件大小.所以是时候下载文件了.设置方法知道导致上述异常.所以我创建一个新的FtpWebRequest?或者无论如何重置请求重用?(结束回复没有任何区别.)
我不明白如何在不重新创建对象的情况下前进.我能做到这一点,但感觉不对劲.所以我在这里发帖,希望能找到正确的方法.
这是(非工作)代码(输入是sURI,sDiskName,sUser和sPwd.):
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(sURI);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = new NetworkCredential(sUser, sPwd);
request.UseBinary = true;
request.UsePassive …Run Code Online (Sandbox Code Playgroud)