Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement