FtpWebRequest 异步调用问题

yangjiaqm 2016-03-07 01:18:29
直接上代码了,以下代码,
private void Window_Loaded(object sender, RoutedEventArgs e)
{

Abc();
Abc();
Abc();
}

private void Abc()
{
FtpWebRequest ftpRequest = null;

try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://xxx.xxx.xxx.xxx/storage/7788"));
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("xxx", "xxx");
ftpRequest.Method = "LIST";// WebRequestMethods.Ftp.GetFileSize;
ftpRequest.Timeout = 5000;
ftpRequest.ReadWriteTimeout = 5000;

ftpRequest.BeginGetResponse(new AsyncCallback(EndGetResponseLengthCallback), null);
MessageBox.Show("Q");
}
catch (Exception ex)
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
finally
{
Console.WriteLine("Step1 Completed");
}
}

private void EndGetResponseLengthCallback(IAsyncResult ar)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => {
MessageBox.Show("A");
}));
FtpWebResponse ftpResponse = null;
try
{
ftpResponse = (FtpWebResponse)ftpRequest.EndGetResponse(ar);
string fileUploadLog = " 服务返回状态码: " + ftpResponse.StatusCode + " 状态描述信息: " + ftpResponse.StatusDescription;
Console.WriteLine(fileUploadLog);
}
catch (Exception ex)
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
}
}

上述代码相信是一个比较简单的获取ftp目录列表的代码,网上很多例子都是这么写的,但是,我实际测试过程中发现,在我断开网络连接的时候,启动程序,在主线程中连续三次调用Abc()方法,只有2次会运行到异步回调函数(EndGetResponseLengthCallback),第三次异步调用后一直没有进回调函数,请大神忙帮助一下,看看这代码如何才能让第三次及以后的异步调用都进回调函数。
...全文
305 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangjiaqm 2016-03-07
  • 打赏
  • 举报
回复
我自己解决了。
System.Net.ServicePointManager.DefaultConnectionLimit = 10;

默认值是2,设置大一点就可以了。
yangjiaqm 2016-03-07
  • 打赏
  • 举报
回复
qbilbo: 没有用的。第一句是BeginGetResponse,你我写的都是一样的,只不过第二个参数不一样罢了。我传递的参数包含了ftpRequest。用你的方式调用我也试了,一样的,连续调用三次,2次有进回调函数,最后1次不进回调。根本不是现在写的代码的问题,如果是在有网络的情况下,会3次都进回调的。
qbilbo 2016-03-07
  • 打赏
  • 举报
回复
改错了,看我的第一句。
yangjiaqm 2016-03-07
  • 打赏
  • 举报
回复
private void Abc() { FtpWebRequest ftpRequest = null; try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://xxx.xxx.xxx.xxx/storage")); ftpRequest.UseBinary = true; ftpRequest.Credentials = new NetworkCredential("xxxx", "xxxx"); ftpRequest.Method = "LIST";// WebRequestMethods.Ftp.GetFileSize; ftpRequest.Timeout = 5000; ftpRequest.ReadWriteTimeout = 5000; FtpRespondLengthAsyncState asyncState = new FtpRespondLengthAsyncState(); asyncState.webRequest = ftpRequest; asyncState.callBack = null; ftpRequest.BeginGetResponse(new AsyncCallback(EndGetResponseLengthCallback), asyncState); } catch (Exception ex) { if (ftpRequest != null) { ftpRequest.Abort(); } } finally { Console.WriteLine("Step1 Completed"); } } private void EndGetResponseLengthCallback(IAsyncResult ar) { if (ar == null || ar.AsyncState == null || !(ar.AsyncState is FtpRespondLengthAsyncState)) { return; } FtpRespondLengthAsyncState asyncState = (FtpRespondLengthAsyncState)ar.AsyncState; if (asyncState == null || asyncState.webRequest == null) { return; } FtpWebRequest ftpRequest = asyncState.webRequest; Action<bool, long> callback = asyncState.callBack; FtpWebResponse ftpResponse = null; try { ftpResponse = (FtpWebResponse)ftpRequest.EndGetResponse(ar); string fileUploadLog = asyncState.Arg1.ToString() + " 服务返回状态码: " + ftpResponse.StatusCode + " 状态描述信息: " + ftpResponse.StatusDescription; Console.WriteLine(fileUploadLog); } catch (Exception ex) { if (ftpRequest != null) { ftpRequest.Abort(); } } finally { if (ftpRequest != null) { ftpRequest.Abort(); } } } 虽然代码已经修改了,但是还是不行。
qbilbo 2016-03-07
  • 打赏
  • 举报
回复
ftpRequest.BeginGetResponse(new AsyncCallback(EndGetResponseLengthCallback), ftpRequest); private void EndGetResponseLengthCallback(IAsyncResult ar) { FtpWebRequest ftpRequest = (FtpWebRequest )ar.AsyncState; ...... }

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧