当我使用以下代码向 FTP 服务器发出请求时。如果请求失败,我会收到错误消息。我不知道为什么。
代码:
try
{
FtpWebRequest request = GetFtpWebResquest(WebRequestMethods.Ftp.ListDirectoryDetails, shareInfo.Uri);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
root = new FtpResource(this, response);
}
catch (Exception e)
{
}
例外:
Unhandled Exception:
System.Net.WebException: Request aborted
at System.Net.FtpWebRequest.CheckIfAborted () [0x00000] in <filename unknown>:0
at System.Net.FtpWebRequest.set_State (RequestState value) [0x00000] in <filename unknown>:0
at System.Net.FtpWebRequest.ProcessRequest () [0x00000] in <filename unknown>:0
at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.WebException: Request aborted
at System.Net.FtpWebRequest.CheckIfAborted () [0x00000] in <filename unknown>:0
at System.Net.FtpWebRequest.set_State (RequestState value) [0x00000] in <filename unknown>:0
at System.Net.FtpWebRequest.ProcessRequest () [0x00000] in <filename unknown>:0
at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0
@@@ ABORTING: INVALID HEAP ADDRESS IN dlfree addr=0x420e3580
_wapi_handle_ref: Attempting to ref unused handle 0x41c
* Assertion at /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/mono/mono/io-layer/wthreads.c:1365, condition `ok' not met
编辑: 为了更好地理解,我实现了一个从 AsyncTask 调用的简单测试方法。AsnycTask 启动一次,除了 GUI 线程和此 AsyncTask 之外,我不使用其他线程。每隔几秒钟,AsyncTask 就会收到我的 ListView 的所有条目并设置状态(离线/在线)。2 - 3 分钟后,我得到了上面未处理的异常。
异步任务:
#region implemented abstract members of AsyncTask
protected override Java.Lang.Object DoInBackground (params Java.Lang.Object[] @params)
{
try
{
_isRunning = true;
while (_isRunning)
{
if (Statics.IsInternetAvailable(_context))
{
AvailableShareAdapter adapter = _context.GetAvailableSharesAdapter();
if (adapter != null)
{
List<ShareInformation> list = adapter.GetAllItems();
foreach(ShareInformation si in list)
{
if (!_isRunning)
break;
si.State = ShareInformation.ShareState.Testing;
_handler.Post(new Java.Lang.Runnable(() => { _context.GetAvailableSharesAdapter().NotifyDataSetChanged(); } ));
if (_coreFacade.SimpleTest(si.Uri, si.UserName, si.Password))
si.State = ShareInformation.ShareState.Online;
else
si.State = ShareInformation.ShareState.Offline;
_handler.Post(new Java.Lang.Runnable(() => { _context.GetAvailableSharesAdapter().NotifyDataSetChanged(); } ));
}
}
Thread.Sleep(60000);
}
}
} catch(Exception e)
{
_coreFacade.Log("DoInBackground", e.ToString());
}
return null;
}
#endregion
简单测试:
public bool SimpleTest(string uri, string username, string password)
{
Console.WriteLine("SimpleTest called");
try {
WebRequest request = HttpWebRequest.Create(uri);
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Timeout = 30 * 1000;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
return true;
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
return false;
}
}
编辑2: 顺便说一句......我只在连接失败时得到这个未处理的异常。如果 FTP 源可用,一切正常。