Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public class Caldevx
  2. {
  3.  
  4. static System.IO.Stream ResponseStream;
  5. static System.Xml.XmlDocument XmlDocument;
  6.  
  7. static string calendarURI = "https://caldav.calendar.yahoo.com/dav/";
  8. static string username = "thanujastech@yahoo.com";
  9. static string password = "******";
  10. static string content = "<?xml version="1.0" encoding="utf-8"?><propfind xlmns"DAV:"><allprop/></propfind>";
  11.  
  12. public void GetCalendars(){
  13. WebHeaderCollection whc = new WebHeaderCollection();
  14. whc.Add(@"Translate", "F");
  15.  
  16. ResponseStream = ExectueMethod(username, password, calendarURI,"PROPFIND", whc, content, "text/xml");
  17.  
  18. XmlDocument = new XmlDocument();
  19. XmlDocument.Load(ResponseStream);
  20. string xmlInner = XmlDocument.InnerXml;
  21.  
  22. XmlDocument innerXmlDocument = new XmlDocument();
  23. innerXmlDocument.LoadXml(xmlInner);
  24.  
  25. XmlNodeList lst = innerXmlDocument.GetElementsByTagName("DAV:href");
  26. List<string> lstICSs = new List<string>();
  27.  
  28. foreach(XmlNode node in lst){
  29. if(node.InnerText.EndsWith(".ics"))
  30. {
  31. lstICSs.Add(node.InnerText.Trim());
  32. }
  33.  
  34. ResponseStream.Close();
  35.  
  36. if (lstICSs.Count > 0)
  37. {
  38. DownloadICS("https//caldav.calendar.yahoo.com", lstICSs);
  39. }
  40. }
  41. }
  42.  
  43. private void DownloadICS(string pathUri, List<string> fileNames)
  44. {
  45. WebClient request = new WebClient();
  46. request.Credentials = new NetworkCredential(username, password);
  47.  
  48. foreach (var file in fileNames)
  49. {
  50. Byte[] data = request.DownloadData(pathUri + file);
  51. var str = System.Text.Encoding.Default.GetString(data);
  52. string path = @"C:" + file.Substring(file.LastIndexOf("/")+1);
  53.  
  54. FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  55. fs.Write(data, 0, data.Length);
  56. fs.Close();
  57.  
  58. DDay.iCal.IICalendarCollection calendars = iCalendar.LoadFromFile(path);
  59.  
  60. foreach (var item in calendars)
  61. {
  62. foreach (Event e in item.Events)
  63. {
  64. Console.WriteLine(e.Description);
  65. }
  66. }
  67. }
  68. }
  69.  
  70. private Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType)
  71. {
  72. HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
  73. httpGetRequest.Credentials = new NetworkCredential(username, password);
  74. httpGetRequest.PreAuthenticate = true;
  75. httpGetRequest.Method = methodName;
  76.  
  77. if (headers != null && headers.HasKeys())
  78. {
  79. httpGetRequest.Headers = headers;
  80. }
  81.  
  82. byte[] optionsArray = Encoding.UTF8.GetBytes(content);
  83.  
  84. httpGetRequest.ContentLength = optionsArray.Length;
  85.  
  86. if (!string.IsNullOrWhiteSpace(contentType))
  87. {
  88. httpGetRequest.ContentType = contentType;
  89. }
  90.  
  91. Stream requestStream = httpGetRequest.GetRequestStream();
  92. requestStream.Write(optionsArray, 0, optionsArray.Length);
  93. requestStream.Close();
  94.  
  95. HttpWebResponse httpGetResponse = (HttpWebResponse)httpGetRequest.GetResponse();
  96. Stream responseStream = httpGetResponse.GetResponseStream();
  97.  
  98. return ResponseStream;
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement