Я пытаюсь добавить несколько файлов с помощью WebDav.Каталог, в который я пытаюсь загрузить файл, пуст.
Я перебираю файлы и отправляю файлы.
1 Добавьте doc1.txt на сервер WebDav с помощью HTTP Put - Успешно всегда, даже еслифайлы уже есть.
2 Добавить doc2.txt на сервер WebDav с помощью HTTP Put - всегда происходит сбой с ошибкой 409.
Не имеет значения, какой файл или порядок я обрабатываю файлыэто всегда терпит неудачу на втором файле.Кто-нибудь есть и идея?
Вот метод, который я использую:
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;
}
}