gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:24 33 4
gpt4 key购买 nike

我已经在我的 Debian Linux 上设置了 WebDAv 服务器,并且工作正常,因为我可以使用 FireFox 和 IE 连接它。

但是当我尝试通过以下代码连接它时,它返回:

"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();

}

我无法理解为什么在通过网络浏览器使用相同的凭据时会发生授权错误。由于存在域,因此我将“域”字段保留为空白。

这是身份验证类型 NLTM 还是其他类型。请帮忙。

谢谢

苏本

最佳答案

在以下帖子中回答:

Sample code for WebDAV PROPFIND

关于c# - 从我的 C# 应用程序访问 webDav 文件夹时出现 401 未经授权的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3107562/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com