C# WebDAV409错误
我正在尝试使用WebDav添加多个文件。我试图上载到的目录为空 我循环浏览文件并发送文件 1使用HTTP Put将doc1.txt添加到WebDav服务器 --即使文件已经存在,也始终成功 2使用HTTP Put将doc2.txt添加到WebDav服务器 --总是失败,出现409错误 无论我处理文件的顺序是什么,第二个文件总是失败。 有人有什么想法吗 以下是我正在使用的方法:C# WebDAV409错误,c#,iis,webdav,C#,Iis,Webdav,我正在尝试使用WebDav添加多个文件。我试图上载到的目录为空 我循环浏览文件并发送文件 1使用HTTP Put将doc1.txt添加到WebDav服务器 --即使文件已经存在,也始终成功 2使用HTTP Put将doc2.txt添加到WebDav服务器 --总是失败,出现409错误 无论我处理文件的顺序是什么,第二个文件总是失败。 有人有什么想法吗 以下是我正在使用的方法: public static bool UploadFile(string url, string filePath) {
public static bool UploadFile(string url, string filePath)
{
if (!File.Exists(filePath))
{
return false;
}
long fileLen = new FileInfo(filePath).Length;
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
Request.Credentials = mCredentials;
Request.Method = WebRequestMethods.Http.Put;
Request.ContentLength = fileLen;
Request.SendChunked = true;
// Specify that overwriting the destination is allowed.
Request.Headers.Add(@"Overwrite", @"T");
Request.AllowWriteStreamBuffering = true;
System.IO.Stream stream = Request.GetRequestStream();
FileStream fileStrem = new FileStream(filePath, FileMode.Open, FileAccess.Read);
int transferRate = 4096;
byte[] data = new byte[transferRate];
int read = 0;
long totalRead = 0;
try
{
do
{
read = fileStrem.Read(data, 0, data.Length);
if (read > 0)
{
totalRead += read;
stream.Write(data, 0, read);
}
} while (read > 0);
}
catch (Exception ex)
{
throw ex;
}
finally
{
stream.Close();
stream.Dispose();
stream = null;
fileStrem.Close();
fileStrem.Dispose();
fileStrem = null;
}
HttpWebResponse Response;
try
{
Response = (HttpWebResponse)Request.GetResponse();
}
catch (WebException e)
{
if (e.Response == null)
{
Debug.WriteLine("Error accessing Url " + url);
throw;
}
HttpWebResponse errorResponse = (HttpWebResponse)e.Response;
//if the file has not been modified
if (errorResponse.StatusCode == HttpStatusCode.NotModified)
{
e.Response.Close();
return false;
}
else
{
e.Response.Close();
Debug.WriteLine("Error accessing Url " + url);
throw;
}
}
//This case happens if no lastmodedate was specified, but the specified
//file does exist on the server.
Response.Close();
if (totalRead == fileLen)
{
return true;
}
else
{
return false;
}
}
这对我来说是个愚蠢的错误。WebDave文档说,“如果PUT导致在没有适当范围的父集合的情况下创建资源,则必须以409(冲突)失败。” 我在文件中循环并连接文件名,而不是仅仅替换文件名 这就是我调用UploadFile的方式:
string url = "http://someurl"
foreach (string file in files)
{
url = url.TrimEnd(new char[] { '/' }) + @"/" + System.IO.Path.GetFileName(file);
UploadFile(url, file);
fileCount++;
}
当我将其更改为此时,它工作:
string url = "http://someurl"
string temp;
foreach (string file in files)
{
temp = url.TrimEnd(new char[] { '/' }) + @"/" + System.IO.Path.GetFileName(file);
UploadFile(temp, file);
fileCount++;
}
7个问题,0%接受。请返回并接受一些答案。显示您正在使用的导致错误的代码。我找到了答案。当我能回答自己的问题时,我会发布解决方案。问题是我在连接url上的文件名,因此服务器上不存在资源。您能否回答您方法中的url示例。比如“什么是uploadefile方法或文件夹名称。你能给出并解释使用你的方法的有效url吗?我得到405方法不允许的错误。”。