使用WebDav协议和VB.NET将文件上载到sharepoint

时间:2012-06-12 08:29:09

标签: .net vb.net upload webdav

我正在尝试将文件上传到sharepoint。授权工作正常,但最终状态为OK,而不是CREATED。最终,文件未创建。我不明白为什么会发生这种情况,因为我使用了似乎适用于其他人的方法(没有抱怨)。这是我正在使用的代码:

Public Sub Create()
    Dim szURL1 = "http://host.domain.com/p/projects/4/Proposal/cze.txt"

    Dim szContent = String.Format("Date/Time: {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString())

    'Define username and password strings.
    Dim domain = "DOMAIN_NAME"
    Dim szUsername = "USER_NAME"
    Dim szPassword = "PASSWORD"

    Dim httpPutRequest As HttpWebRequest = DirectCast(WebRequest.Create(szURL1), HttpWebRequest)
    httpPutRequest.Credentials = New NetworkCredential(szUsername, szPassword, domain)
    httpPutRequest.PreAuthenticate = True
    httpPutRequest.Method = "PUT"
    httpPutRequest.Headers.Add("Overwrite", "T")
    httpPutRequest.ContentLength = szContent.Length

    'Optional, but allows for larger files.
    httpPutRequest.SendChunked = True

    Dim requestStream = httpPutRequest.GetRequestStream()

    'Write the string to the destination as a text file.
    requestStream.Write(System.Text.Encoding.UTF8.GetBytes(DirectCast(szContent, String)), 0, szContent.Length)

    'Close the request stream.
    requestStream.Close()

    'Retrieve the response.
    Dim httpPutResponse As HttpWebResponse = DirectCast(httpPutRequest.GetResponse(), HttpWebResponse)
    Debug.WriteLine("PUT Response #1: {0}", httpPutResponse.StatusDescription)
End Sub

这会产生:PUT Response #1: OK而非PUT Response #1: CREATED

我从http://blogs.iis.net/robert_mcmurray/archive/2010/02/09/sending-webdav-requests-in-net.aspx获取代码并将其翻译为VB,但我认为翻译不是问题。

有什么想法吗?

编辑: 我检查了原始的C#代码,结果是一样的。可能是什么问题?

2 个答案:

答案 0 :(得分:1)

刚刚用另一种语言处理此问题,我建议禁用 SendChunked 。这两者不应该是混合的,SharePoint 2010似乎不喜欢上传未知的大小。

答案 1 :(得分:0)

我不知道为什么以上不起作用。我写了这个简单的代码并且它有效:

var client = new WebClient();
client.Credentials = new NetworkCredential(username, password, domain);
client.UploadData("http://host.domain.com/p/projects/4/Proposal/cze.txt", "PUT", Encoding.UTF8.GetBytes(DateTime.Now.ToString()));

如果有人可以回答原来的问题,我会对其进行投票并接受它作为答案。

相关问题