使用Http请求和DDay.iCal将事件添加到Davical服务器

时间:2014-12-22 19:23:21

标签: httprequest put http-status-code-405 caldav

我正在尝试将本地数据库中的事件添加到Davical服务器(事实上,这应该适用于任何CalDav服务器,只要它符合CalDav协议)...

根据我的阅读here,我可以发送一个PUT请求来添加VCALENDAR集合中包含的事件......所以这就是我尝试做的事情:

     try {
        // Create the HttpWebRequest object
        HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("http://my_caldav_srv/davical.php/user/mycalendar");

        // Add the network credentials to the request
        Request.Credentials = new NetworkCredential(usr, pwd);

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

        // some headers - I MAY BE MISSING THINGS HERE???
        Request.Headers.Add("Overwrite", "T");

        // set the body of the request...
        Request.ContentLength = bodyStr.Length;
        Stream reqStream = Request.GetRequestStream();
        // Write the string to the destination as a text file.
        reqStream.Write( Encoding.UTF8.GetBytes(body), 0, body.Length);

        // Set the content type header.
        Request.ContentType = contentType.Trim();

        // Send the method request and get the response from the server.
        Response = (HttpWebResponse)Request.GetResponse();
      }
      catch (Exception e) {
        throw new Exception("Caught error: " + e.Message, e);
      }

我发送的身体实际上是一个日历:

BEGIN:VCALENDAR
  VERSION:2.0
  CALSCALE:GREGORIAN
  METHOD:PUBLISH
  PRODID:-//davical.org//NONSGML AWL Calendar//EN 
  X-WR-CALNAME:My Calendar
END:VCALENDAR

由于某种原因我无法理解,使用" PUT"返回错误(405)方法不允许。 PUSH返回(500)内部服务器错误,但查看调试详细信息,原因与PUT情况相同......

在服务器端调试时,我发现原因是在 caldav-PUT-vcalendar.php 中违反了以下条款:

$c->readonly_webdav_collections

嗯,首先,让我提一下,使用在Lightning中输入的SAME凭据,我可以添加/删除事件,并且在管理界面上,我实际上确保向用户授予所有权限。因此,我感到惊讶......

任何帮助都将非常感谢! 亲切的问候, NIK

1 个答案:

答案 0 :(得分:0)

好的,我明白了......

原因是必须将事件发送到某个EVENT地址....

即。 “url”不是集合的地址,而是EVENT的地址......

所以使用以下地址的相同代码有效:

string url="http://my_server/caldav.php/username/calendarpath/_my_event_id.ics";

有人知道是否可以一次插入/删除多个事件???

相关问题