How to check FtpWebRequest for errors How to check FtpWebRequest for errors powershell powershell

How to check FtpWebRequest for errors


Yeah, you want to grab the FTPWebResponse object from the request object...like this:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); request.Method = WebRequestMethods.Ftp.UploadFile; FtpWebResponse response = (FtpWebResponse) request.GetResponse(); request.KeepAlive = false; byte[] fileraw = File.ReadAllBytes("CompleteLocalPath"); try {     Stream reqStream = request.GetRequestStream();     reqStream.Write(fileraw, 0, fileraw.Length);     reqStream.Close(); }   catch (Exception e) {     response = (FtpWebResponse) request.GetResponse();     // Do something with response.StatusCode     response.Close(); }

You will want to check Ftp.WebResponse.StatusCode.

There are a quite a few members in StatusCode that can be returned, so checking against it can be tricky.

Here's a list of codes/descriptions that might be returned:

FtpStatusCode

EDIT: If something goes wrong with a transfer it should throw an exception when you fire up a stream writer. What you can do is wrap a try-catch around it all, and if something goes wrong you will be able to get the status code and print it out to whatever log medium you are using so you can see what the specific problem is. I've amended the code above to reflect all of this (Using just one way of transferring, you can use your own).