Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;远程服务器返回错误:(401)Unauthorized";尝试上载文件时出错_C#_Asp.net_Sharepoint_Put_Http Status Code 401 - Fatal编程技术网

C# &引用;远程服务器返回错误:(401)Unauthorized";尝试上载文件时出错

C# &引用;远程服务器返回错误:(401)Unauthorized";尝试上载文件时出错,c#,asp.net,sharepoint,put,http-status-code-401,C#,Asp.net,Sharepoint,Put,Http Status Code 401,我发现了很多关于(401)未经授权的错误的问题,但没有一个向我解释如何诊断这个问题 我创建了一个新的MVC ASP.net应用程序来学习如何将文件上载到sharepoint文件夹。到目前为止,我似乎无法从中复制文件 C:\testfolder\file.txt到 C:\testfolder\UploadedFiles 以下是我在源文件路径和目标文件夹中发送的方法: //Flag to indicate whether file was uploaded successfuly or not

我发现了很多关于(401)未经授权的错误的问题,但没有一个向我解释如何诊断这个问题

我创建了一个新的MVC ASP.net应用程序来学习如何将文件上载到sharepoint文件夹。到目前为止,我似乎无法从中复制文件 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中的应用程序池帐户。我仍然不确定问题出在哪里。

这与代理设置不同,不是吗

是否需要将默认代理设置添加到服务器

WebRequest 
您还需要添加吗

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

你的网页配置

这可能会有所帮助


我决定尝试另一种方法,并使用httpposted file和.saveAs()描述的

该错误意味着您甚至无权请求试图执行文件移动的URL;这与文件移动代码本身无关-该代码甚至没有运行。@AndrewBarber,我用本地文件复制到本地C驱动器时运气不佳。您可能可以尝试
System.Net.CredentialCache.DefaultNetworkCredentials