无法将类型为"System.Net.FileWebRequest"的对象转换为类型为"System.Net.HttpWebRequest"的对象 [英] Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'

查看:95
本文介绍了无法将类型为"System.Net.FileWebRequest"的对象转换为类型为"System.Net.HttpWebRequest"的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试测试上传到FTP时出现上述错误.但是,当我尝试从本地计算机运行此代码时,它给出了错误.好心提醒.

I'm getting the above error when trying to test on upload to FTP. But when I'm trying run this code from my local machine, it is giving error. Kindly advise.

下面是我的代码:

 static void Main(string[] args)
    {

        var yourListOfFilePaths = Directory.GetFiles(filepath);

        using (ZipFile zip = new ZipFile())
        {
            foreach (string filePath in yourListOfFilePaths)
            {
                zip.AddFile(filePath);    // FILE PATH LOCATION / WHICH FOLDER FILES YOU WANTED TO ZIP
                zip.Password = "abc1234"; // CHANGE YOUR PASSWORD HERE 
            }
            zip.Save(ZipPath + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("http://www.bitrix24.com/" + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("jayden@bitrix24.com", "abc123");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(ZipPath + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");
            byte[] fileContents = File.ReadAllBytes("filepath");
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            request.KeepAlive = false;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();

        }
    }

推荐答案

此:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("http://www.bitrix24.com/"
                              + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");

是您的问题.您要发送的地址以"http"开头,而不是"ftp".

Is your problem. You're sending an address which starts with "http" instead of "ftp.

更改您的URL:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.bitrix24.com/" + 
                                "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");

这篇关于无法将类型为"System.Net.FileWebRequest"的对象转换为类型为"System.Net.HttpWebRequest"的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆