【问题标题】:"The remote server returned an error: (401) Unauthorized" error when trying to upload a file尝试上传文件时出现“远程服务器返回错误:(401) Unauthorized”错误
【发布时间】:2012-08-29 21:40:45
【问题描述】:

我发现了很多关于 (401) Unauthorized error 的问题,但没有一个向我解释如何诊断问题。

我创建了一个新的 MVC ASP.net 应用程序来学习如何将文件上传到共享点文件夹。截至目前,我似乎无法从 C:\testfolder\file.txt 到 C:\testfolder\UploadedFiles

这是我在源文件路径和目标文件夹中发送的方法:

 //Flag to indicate whether file was uploaded successfuly or not
        bool isUploaded = true;
        try
        {
            // Create a PUT Web request to upload the file.
            WebRequest request = WebRequest.Create(targetDocumentLibraryPath);

            //Set credentials of the current security context
            request.PreAuthenticate = true;
            request.UseDefaultCredentials = true;
            ICredentials credentials = new NetworkCredential( "Username", "password", "Domain"); //I used my username and password here
            request.Credentials = credentials;
            //request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "PUT";

            // Create buffer to transfer file
            byte[] fileBuffer = new byte[1024];

            // Write the contents of the local file to the request stream.
            using (Stream stream = request.GetRequestStream())
            {
                //Load the content from local file to stream
                using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
                {
                    //Get the start point
                    int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
                    for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
                    {
                        stream.Write(fileBuffer, 0, i);
                    }

                }
            }

            // Perform the PUT request
            WebResponse response = request.GetResponse();

            //Close response
            response.Close();
        }
        catch (Exception ex)
        {
            //Set the flag to indiacte failure in uploading
            isUploaded = false; //The remote server returned an error: (401) Unauthorized.
        }

        //Return the final upload status
        return isUploaded; 
    }

我尝试了以下方法:

  • 更改文件夹的权限,以便所有用户都可以对其进行写入/读取。
  • 使用其他文件夹位置(SharePoint、本地驱动器、网络驱动器)将文件上传到。

知道如何解决这个问题吗?任何有关如何调试问题的预期也表示赞赏。

更新:可能是我的帐户在 IIS 中设置为应用程序池帐户。我仍然不确定问题是什么。

【问题讨论】:

  • 该错误意味着您甚至无权请求尝试执行文件移动的 URL;它与文件移动代码本身无关 - 该代码甚至没有运行。
  • @AndrewBarber,我尝试使用本地文件复制到本地 C 盘,但没有成功。
  • 你可以试试System.Net.CredentialCache.DefaultNetworkCredentials

标签: c# asp.net sharepoint put http-status-code-401


【解决方案1】:

这不会类似于代理设置吧?

您是否需要将默认代理设置添加到您的

WebRequest 

还需要加吗

<system.net> 
  <defaultProxy useDefaultCredentials="true" /> 
</system.net> 

到您的网络配置?

这可能会有所帮助。

How should I set the default proxy to use default credentials?

【讨论】:

    【解决方案2】:

    我决定尝试另一种方法并使用 httpposted 文件和 .saveAs() 描述 here

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多