我有这个代码,但我得到标题中描述的错误:“远程服务器返回错误:(405)方法不允许。”
** 我现在用 POST 替换了 PUT **
如果我用“POST”替换“PUT”,它似乎可以工作,因为我没有收到错误,但它不会上传任何文件。我正在尝试将文件上传到 sharepoint (office 365) 中的文档库
public static void UploadTest()
{
WebClient w = new WebClient();
w.Credentials = new NetworkCredential("username", "password");
var ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
w.Headers["Accept"] = "/";
w.Headers.Add(HttpRequestHeader.UserAgent, ua);
byte[] bFile = System.IO.File.ReadAllBytes(@"C:\t.txt");
string ulr = @"http://www.website.com/uploadfolder/";
System.Uri oUri = new System.Uri(ulr);
try
{
w.UploadData(oUri, "POST", bFile);
w.UploadDataCompleted += new UploadDataCompletedEventHandler(oWebClient_UploadDataCompleted);
Console.WriteLine("Uri:" + oUri);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Console.ReadLine();
}
}