有人可以为C#提供CALDAV示例吗?提前致谢。我正在努力寻找一个除了这个之外的例子。
https://ovaismehboob.wordpress.com/2014/03/30/sync-calendar-events-using-caldav/
但是id对我不起作用。 我收到以下错误。
{"远程服务器返回错误:(400)错误请求。"}
这是代码,
public class Caldevx
{
static System.IO.Stream ResponseStream;
static System.Xml.XmlDocument XmlDocument;
static string calendarURI = "https://caldav.calendar.yahoo.com/dav/";
static string username = "thanujastech@yahoo.com";
static string password = "******";
static string content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xlmns\"DAV:\"><allprop/></propfind>";
public void GetCalendars(){
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add(@"Translate", "F");
ResponseStream = ExectueMethod(username, password, calendarURI,"PROPFIND", whc, content, "text/xml");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
string xmlInner = XmlDocument.InnerXml;
XmlDocument innerXmlDocument = new XmlDocument();
innerXmlDocument.LoadXml(xmlInner);
XmlNodeList lst = innerXmlDocument.GetElementsByTagName("DAV:href");
List<string> lstICSs = new List<string>();
foreach(XmlNode node in lst){
if(node.InnerText.EndsWith(".ics"))
{
lstICSs.Add(node.InnerText.Trim());
}
ResponseStream.Close();
if (lstICSs.Count > 0)
{
DownloadICS("https//caldav.calendar.yahoo.com", lstICSs);
}
}
}
private void DownloadICS(string pathUri, List<string> fileNames)
{
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(username, password);
foreach (var file in fileNames)
{
Byte[] data = request.DownloadData(pathUri + file);
var str = System.Text.Encoding.Default.GetString(data);
string path = @"C:\" + file.Substring(file.LastIndexOf("/")+1);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(data, 0, data.Length);
fs.Close();
DDay.iCal.IICalendarCollection calendars = iCalendar.LoadFromFile(path);
foreach (var item in calendars)
{
foreach (Event e in item.Events)
{
Console.WriteLine(e.Description);
}
}
}
}
private Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType)
{
HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
httpGetRequest.Credentials = new NetworkCredential(username, password);
httpGetRequest.PreAuthenticate = true;
httpGetRequest.Method = methodName;
if (headers != null && headers.HasKeys())
{
httpGetRequest.Headers = headers;
}
byte[] optionsArray = Encoding.UTF8.GetBytes(content);
httpGetRequest.ContentLength = optionsArray.Length;
if (!string.IsNullOrWhiteSpace(contentType))
{
httpGetRequest.ContentType = contentType;
}
Stream requestStream = httpGetRequest.GetRequestStream();
requestStream.Write(optionsArray, 0, optionsArray.Length);
requestStream.Close();
HttpWebResponse httpGetResponse = (HttpWebResponse)httpGetRequest.GetResponse();
Stream responseStream = httpGetResponse.GetResponseStream();
return ResponseStream;
}
}