【问题标题】:WebDAV Exchange 2003 failing when specifying a date指定日期时 WebDAV Exchange 2003 失败
【发布时间】:2012-04-10 12:29:10
【问题描述】:

我正在使用以下代码从 Exchange 2003 服务器检索电子邮件。周五一切正常,但现在失败了。

通过一些调查,我已将其范围缩小到 targetDate 变量。似乎日期是在 4 月,它会从服务器返回 400 失败。我已经评论了这条线并尝试了各种日期 2012-3-29、2012-4-1、2012-4-10(今天),而 4 月的日期似乎失败了。

某种恶心的愚人节玩笑?

代码本身来源于这篇文章:http://www.codeproject.com/Articles/42458/Using-Exchange-2003-with-Webdav-Send-Retrieve-Atta

    public XmlDocument GetMailAll()
    {
        HttpWebRequest request = default(HttpWebRequest);
        HttpWebResponse response = default(HttpWebResponse);
        string rootUri = null;
        string query = null;
        byte[] bytes = null;

        Stream requestStream = default(Stream);
        Stream responseStream = default(Stream);
        XmlDocument xmlDoc = default(XmlDocument);
        xmlDoc = new XmlDocument();
        try
        {
            DateTime targetDateTime = DateTime.Today.AddDays(-5);
            String targetDate = ""+targetDateTime.Year + "-" + targetDateTime.Month + "-" + targetDateTime.Day;

            rootUri = server + "/Exchange/" + alias + "/" + inbox;
            query = "<?xml version=\"1.0\"?>"
                        + "<D:searchrequest xmlns:D = \"DAV:\" xmlns:m=\"urn:schemas:httpmail:\">"
                        + "<D:sql>SELECT \"urn:schemas:httpmail:hasattachment\", \"DAV:displayname\", "
                        + "\"urn:schemas:httpmail:from\", \"urn:schemas:httpmail:subject\", "
                        //+ "\"urn:schemas:httpmail:htmldescription\","  //Return full body (not necessary right now)
                        + "\"urn:schemas:httpmail:datereceived\", \"urn:schemas:httpmail:read\" FROM \"" + rootUri
                        + "\" WHERE \"DAV:ishidden\" = false "
                        + "AND \"DAV:isfolder\" = false " 
                        //+ "AND \"urn:schemas:httpmail:read\" = false"
                        + "AND \"urn:schemas:httpmail:datereceived\" >= CAST(\"" + targetDate + "T00:00:000Z\" AS 'dateTime.tz')"
                        + "</D:sql></D:searchrequest>";
            request = (HttpWebRequest)WebRequest.Create(rootUri);
            request.Timeout = 5000;
            request.Credentials = new NetworkCredential(alias, password, domain);
            request.Method = "SEARCH";
            request.ContentType = "text/xml";
            request.Headers.Add("Translate", "F");
            bytes = System.Text.Encoding.UTF8.GetBytes(query);
            request.ContentLength = bytes.Length;

            requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            response = (HttpWebResponse)request.GetResponse();

            authCookies = new List<Cookie>();
            foreach(Cookie cookie in response.Cookies)
            {
                authCookies.Add(cookie);
            }

            responseStream = response.GetResponseStream();
            xmlDoc.Load(responseStream);
            responseStream.Close();
        }
        catch (WebException ex)
        {
            if (ex.Response == null)
            {
                throw new Exception();
            }
            else if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new ExchangeCatastrophicException();
            }
            else
            {
                throw new ExchangeFailedException();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
        return xmlDoc;
    }

【问题讨论】:

    标签: c# exchange-server webdav


    【解决方案1】:

    终于搞定了。要转换的确切 targetDate 包括 T00:00:..Z 部分必须与 http://msdn.microsoft.com/en-us/library/aa123600%28v=exchg.65%29.aspx 上的规范匹配

    我现在有:

    DateTime targetDateTime = DateTime.Today;
    string targetDate = targetDateTime.ToString("yyyy-MM-dd");
    

    剪辑

    + "AND \"urn:schemas:httpmail:datereceived\" >= CAST(\"" + targetDate + "T00:00:00Z" + "\" AS 'dateTime.tz')"
    

    请注意,秒部分是两个零而不是三个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多