Use Ftpwebrequest with Uri https

Asked

Viewed 705 times

0

Srs(as), good afternoon,

I am creating a service that will upload some files to an FTP link, however this is like HTTPS (https://path/path).

occurs that the class Ftpwebrequest does not allow the use of Uri hhtp/https, the error occurs below.

Could help?

Unable to cast Object of type 'System.Net.Httpwebrequest' to type 'System.Net.Ftpwebrequest'.

below send the code snippet I am 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://camio.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

2 answers

1

Ladies and gentlemen(s), I have solved the problem...

Using the component WebClient and UploadFile.

Thank you so much for your help!!!!

follows 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;
        }
  • Great - using HTTP (not FTP) worked!

1


Are you sure the link protocol is FTP itself, 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 the call to 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 return an object of the 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 speaking FTP (443 is the port default of the HTTPS protocol).

  • Thanks for the help, but in this case returns error in . Getrequeststream(). using (Stream Writer = request.Getrequeststream()) { Writer.Write(fileContents, 0, fileContents.Length); } Sponse = (Ftpwebresponse)request.Getresponse(); The requested URI is invalid for this FTP command.

  • One thing you can do is try to use the browser to go on https://caminho. If you see a page, then the server is not speaking FTP - it is speaking HTTP. And you cannot use an FTP client to talk to it.

  • Carlos, the address that was sent requires user and password to login, and after access is displayed a page "equal" to FTP. I say that in this case it is FTP, because I downloaded the link from the Smartftp APP and did Up/Down Loads of files.

  • Take a look at Fiddler (or your browser’s Debugger, in the "network" section) to find out what protocol is being used on the "FTP equal" page - even try to "download" one of the files there. If it’s FTP even then I don’t know what’s going on (and I’ll delete my answer)

  • HTTPS :-(... , other details. Request URL: https://pathcom.com/input/ Request Method: GET Status Code: 200 / OK - Request Headers Accept: text/html, application/xhtml+xml, image/jxr, / Accept-Encoding: gzip, deflate Accept-Language: en-US Authorization: ******** Connection: Keep-Alive Host: pathcom.com Referer: https://pathcom.com/input/ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) Like Gecko Connection: Keep-Alive Content-Encoding: gzip Content-Length: 358 Content-Type: text/html; charset=UTF-8

  • This request is HTTP, not FTP... If you can use a network capture tool (Fiddler type) you can see if Smartftp is using HTTP or FTP even

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.