Use FtpWebRequest with uri https

0

Sirs, good afternoon,

I'm creating a service that will upload some files to an FTP link, but this is like HTTPS ( link ).

It occurs that the FtpWebRequest class does not allow the use of uri hhtp / https, with the error below occurring.

Could you help?

  

Unable to cast object of type 'System.Net.HttpWebRequest' to type   'System.Net.FtpWebRequest'.

below, I'm sending the code snippet I'm using.

            string uriPath = "https//caminho/path";
            FtpWebRequest request;
            FtpWebResponse response;
            try
            {
                request = (FtpWebRequest)WebRequest.Create(uriPath);//Aqui ocorre o erro!!!!
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(userCred, passCred);
                request.UsePassive = true;

                DirectoryInfo dir = new DirectoryInfo(caminhoPath);

                foreach (FileInfo file in dir.GetFiles())
                {
                    FileInfo arquivo = new FileInfo(file.FullName);
                    byte[] fileContents = new byte[arquivo.Length];

                    using (FileStream fr = arquivo.OpenRead())
                    {
                        fr.Read(fileContents, 0, Convert.ToInt32(arquivo.Length));
                    }

                    using (Stream writer = request.GetRequestStream())
                    {
                        writer.Write(fileContents, 0, fileContents.Length);
                    }
                    response = (FtpWebResponse)request.GetResponse();
                }
            }
            catch (WebException webEx)

Using the address as " ftp://path.com:443/path "

Returns the error in the GetRequestStream () call.

                using (Stream writer = request.GetRequestStream())//Erro aqui!!!
                {
                    writer.Write(fileContents, 0, fileContents.Length);
                }
                response = (FtpWebResponse)request.GetResponse();
  

The requested URI is invalid for this FTP command

    
asked by anonymous 09.11.2015 / 17:30

2 answers

1

Are you sure that the link protocol is same FTP, not HTTP (S)? If an address is given as http://dominio.com/path , then the server is "talking" HTTP. And since this is a (virtually) universal truth, all APIs assume this, which is why calling WebRequest.Create("http://caminho/path") will return a HttpWebRequest . The solution would really be for the server administrator to send you a correct URL (including the schema).

Having said the above, if it is really the case that your address goes to an FTP server, then you can change the scheme of the protocol, which will cause the call to Create to return an object of type FtpWebRequest :

string uriPath = "https//caminho/path";
FtpWebRequest request;
try
{
    string ftpUriPath = uriPath.Replace("https://", "ftp://");
    request = (FtpWebRequest)WebRequest.Create(ftpUriPath);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    // ...
}
catch (WebException webEx) { ... }

Note that you may have to specify the port ( ftp://caminho:443/path ) if your HTTPS server is talking about FTP (443 is the default port).

    
09.11.2015 / 18:02
1

Srs, I was able to solve the problem ...

Using the component WebClient and UploadFile .

Thanks for the help !!!!

follow the code below.

private void SendFiles(string fileName)
    {
        CredentialCache credCache;
        Uri uri;
        NetworkCredential netCredential;
        DirectoryInfo iDirectory;
        byte[] response;
        try
        {
            using (WebClient webClient = new WebClient())
            {

                credCache = new CredentialCache();
                uri = new Uri(uriPath);
                netCredential = new NetworkCredential(userCred, passCred);

                credCache.Add(uri, AuthenticationSchemes.Basic.ToString(), netCredential);
                webClient.Credentials = credCache;
                response = webClient.UploadFile(string.Format("{0}{1}", uri, Path.GetFileName(fileName)), "PUT", fileName);
                System.Text.Encoding.ASCII.GetString(response); 
            }

        }
        catch (WebException webEx)
        {
            throw webEx;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
10.11.2015 / 12:52