c# - Detecting valid FTP connection -
i saw thread @ how check ftp connection? , tried of suggestions. here's have:
private void isftploginsuccessful(ftpclient ftpclient, string ftpfolder, string ftpusername, string ftppassword) { ftpwebrequest requestdir = (ftpwebrequest)ftpwebrequest.create(ftpfolder); requestdir.credentials = new networkcredential(ftpusername, ftppassword); try { log(loglevel.debug, "just entered try block"); requestdir.method = webrequestmethods.ftp.listdirectorydetails; webresponse response = requestdir.getresponse(); log(loglevel.debug, "good"); } catch (exception ex) { log(loglevel.debug, "bad"); } }
if username / password invalid, last thing that's logged "just entered try block". code somehow silently errors-out , never logs "bad". if credentials valid, continues execution , logs "good".
i suppose gives me boolean whether log-in successful. there way distinguish whether credentials bad or if it's ftp server that's not responding?
thank you!
you should using status codes in response ftpwebrequest.
you can see full list here
in case of implementation
private void isftploginsuccessful(ftpclient ftpclient, string ftpfolder, string ftpusername, string ftppassword) { ftpwebrequest requestdir = (ftpwebrequest)ftpwebrequest.create(ftpfolder); requestdir.credentials = new networkcredential(ftpusername, ftppassword); log(loglevel.debug, "just entered try block"); requestdir.method = webrequestmethods.ftp.listdirectorydetails; ftpwebresponse response = (ftpwebresponse)requestdir.getresponse(); if(response.statusdescription != ftpstatuscode.commandok || response.statusdescription != ftpstatuscode.fileactionok) log(loglevel.debug, "bad"); }
here's sample code on msdn.
public static bool makedirectoryonserver (uri serveruri) { // serveruri should start ftp:// scheme. if (serveruri.scheme != uri.urischemeftp) { return false; } // object used communicate server. ftpwebrequest request = (ftpwebrequest)webrequest.create (serveruri); request.keepalive = true; request.method = webrequestmethods.ftp.makedirectory; //getting status description ftpwebresponse response = (ftpwebresponse)request.getresponse (); console.writeline ("status: {0}", response.statusdescription); return true; }
Comments
Post a Comment