gpt4 book ai didi

c# - 无法将类型为 'System.Net.FileWebRequest' 的对象转换为类型 'System.Net.HttpWebRequest'

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:58 43 4
gpt4 key购买 nike

我在尝试测试上传到 FTP 时遇到上述错误。但是当我尝试从我的本地机器运行这段代码时,它给出了错误。好心提醒。

下面是我的代码:

 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”开头。

更改您的网址:

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

关于c# - 无法将类型为 'System.Net.FileWebRequest' 的对象转换为类型 'System.Net.HttpWebRequest',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28084108/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com