1

我正在尝试将文件上传到共享点。授权工作正常,但最终以 statusOK而不是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# 代码,结果是一样的。可能是什么问题呢?

4

2 回答 2

2

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

于 2012-10-01T15:38:01.717 回答
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()));

如果有人可以回答原始问题,我会投票并接受它作为答案。

于 2012-06-12T09:06:52.337 回答