WebDav 409错误 [英] WebDav 409 Error

查看:330
本文介绍了WebDav 409错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebDav添加多个文件。我试图上传到的目录是空的。

I am trying to add multiple files using WebDav. The directory I am trying to upload to is empty.

我遍历文件并发送文件。

I loop through the files and sent the files.

1使用HTTP将doc1.txt添加到WebDav服务器Put
- 即使文件已经存在,也会成功。

1 Add doc1.txt to WebDav Server using HTTP Put -- Success always even if the files is already there.

2将doc2.txt添加到WebDav服务器使用HTTP Put
- 总是因409错误而失败。

2 Add doc2.txt to WebDav Server using HTTP Put -- Always fails with a 409 error.

我处理文件或命令无关紧要文件。
任何人有想法吗?

It does not matter what file or order I process the files it always fails on the second file. Anyone have and idea?

以下是我使用的方法:

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(冲突)失败。。

This is a stupid mistake on my part. WebDave documentation say, "A PUT that would result in the creation of a resource without an appropriately scoped parent collection MUST fail with a 409 (Conflict).".

我是正在循环我的文件并连接文件名而不是只替换文件名。

Well I was looping through my files and concatenating the file name instead of just replacing the file name.

这就是我调用UploadFile的方式:

This is how I was calling UploadFile:

string url = "http://someurl"

foreach (string file in files)
{
    url = url.TrimEnd(new char[] { '/' }) + @"/" + System.IO.Path.GetFileName(file);
    UploadFile(url, file);
    fileCount++;
}

当我将其改为此工作时:

When I changed it to this it work:

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++;
}

这篇关于WebDav 409错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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