在文件上传时获取401 Unauthorized错误

时间:2013-10-07 22:33:40

标签: c# asp.net file httpwebrequest

我有一个asp.net应用程序,它将文件提交到sharepoint文档库。 每次尝试提交我都会得到一个

  

远程服务器返回错误:(401)未经授权

这是c#代码

                    byte[] buffer = new byte[length];
                    postedFile.InputStream.Read(buffer, 0, length);


                    Uri address = new Uri(url);
                    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
                    request.Method = "POST";

                    request.UseDefaultCredentials = true;

                    request.Accept = "application/json;odata=verbose";
                    request.Headers.Add("X-RequestDigest", dvalue);
                    request.ContentType = "application/json; charset=utf-8";
                    request.ContentLength = length;
                    using (Stream postSTream = request.GetRequestStream())
                    {
                        postSTream.Write(buffer, 0, length);
                    }
                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                    }                       

这在我的本地计算机上工作正常,但是当我将它部署到我们的测试环境时,它会引发401错误。 IIS配置为匿名身份验证。我认为这与文件上传有关?任何见解或想法都表示赞赏!

由于

1 个答案:

答案 0 :(得分:0)

您应该为应用程序池用户帐户授予写文件夹权限。