简体   繁体   English

从我的C#应用​​程序访问webDav文件夹时出现401未经授权的错误?

[英]Getting 401 unauthorized error while accessing webDav folder from my C# application?

I have set up WebDAv server on my Debian linux and this is working fine as I am able to connect it by using FireFox and IE. 我已经在Debian linux上设置了WebDAv服务器,并且可以正常使用,因为我可以使用FireFox和IE进行连接。

But while I am trying to connect it by the following code, it returns: 但是,当我尝试通过以下代码连接它时,它返回:

"The remote server returned and error:(401) Unauthorized error"

try
{
                // Build the SQL query.
                strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >";
                strQuery += "<D:sql>SELECT \"DAV:href\" FROM scope('hierarchical traversal of \"";
                strQuery += strRootURI + "\"')</D:sql></D:searchrequest>";

                // Create a new CredentialCache object and fill it with the network
                // credentials required to access the server.
                MyCredentialCache = new System.Net.CredentialCache();
                MyCredentialCache.Add(new System.Uri(strRootURI),
                   "NTLM",
                   new System.Net.NetworkCredential(strUserName, strPassword)
                   );

                // Create the HttpWebRequest object.
                Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);

                // Add the network credentials to the request.
                Request.Credentials = MyCredentialCache;

                // Specify the method.
                Request.Method = "SEARCH";

                // Encode the body using UTF-8.
                bytes = Encoding.UTF8.GetBytes((string)strQuery);

                // Set the content header length.  This must be
                // done before writing data to the request stream.
                Request.ContentLength = bytes.Length;

                // Get a reference to the request stream.
                RequestStream = Request.GetRequestStream();

                // Write the SQL query to the request stream.
                RequestStream.Write(bytes, 0, bytes.Length);

                // Close the Stream object to release the connection
                // for further use.
                RequestStream.Close();

                // Set the content type header.
                Request.ContentType = "text/xml";

                // Send the SEARCH method request and get the
                // response from the server.
                Response = (HttpWebResponse)Request.GetResponse();

                // Get the XML response stream.
                ResponseStream = Response.GetResponseStream();

                // Create the XmlTextReader object from the XML
                // response stream.
                XmlReader = new XmlTextReader(ResponseStream);

                // Read through the XML response, node by node.
                while (XmlReader.Read())
                {
                    // Look for the opening DAV:href node.  The DAV: namespace is
                    //typically assigned the a: prefix in the XML response body.
                    if (XmlReader.Name == "a:href")
                    {
                        // Advance the reader to the text node.
                        XmlReader.Read();

                        // Display the value of the DAV:href text node.
                        Console.WriteLine("Value: " + XmlReader.Value);
                        Console.WriteLine("");

                        //Advance the reader to the closing DAV:href node.
                        XmlReader.Read();
                    }
                }

                // Clean up.
                XmlReader.Close();
                ResponseStream.Close();
                Response.Close();

            }

I am unable to understand why the anuathorized error is happening while the same credentials are working throgh web browser. 我无法理解为什么在相同的凭据通过Web浏览器正常工作时为什么会发生anuathorized错误。 There are domains so I left the Domain field as blank. 有域,因此我将“域”字段留为空白。

Is this the Authentication type NLTM or something else. 这是身份验证类型NLTM还是其他类型。 Please help . 请帮忙 。

Thanks, 谢谢,

Subhen 苏本

Answered at the following Post : 在以下帖子中回答:

Sample code for WebDAV PROPFIND WebDAV PROPFIND的示例代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:远程服务器返回错误:(401)未经授权。 ,同时将我的网站评论发布到Twitter。 使用C# - Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C# 在C#窗口应用程序中SSRS报告呈现中出现错误:请求失败,HTTP状态为401:未经授权 - Getting Error in SSRS report rendering in C# window application: The request failed with HTTP status 401: Unauthorized 使用来自 Sharepoint 和 C# 的链接时出错((401)未经授权) - Error when use link from Sharepoint with C# ( (401) unauthorized ) 从ajax调用本地C#Web服务,错误401未经授权 - Call a local C# webservice from ajax , 401 error Unauthorized 应用程序更新程序-不断收到401未经授权的错误 - Application Updater - Keep getting 401 Unauthorized error 通过Intuit API访问quickBooks数据时,.NET中出现“ Unauthorized-401”错误 - Getting “Unauthorized-401” Error in .NET while accessing quickBooks data through Intuit API httpclient api 在 c# 中出现未经授权的 401 错误 - httpclient api unauthorized 401 error in c# 我在 c# wpf 中使用 spotify api 未经授权收到错误 401 - im getting a error 401 unauthorized using the spotify api in c# wpf 使用C#上传文件时出现远程服务器错误(401)未经授权 - the remote server error (401) Unauthorized while uploading a file using c# 链接到C#中的当前状态更新,获取错误(401)未经授权 - Linked In current-status update in c#, Getting Error (401) Unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM