簡體   English   中英

指定日期時 WebDAV Exchange 2003 失敗

[英]WebDAV Exchange 2003 failing when specifying a date

我正在使用以下代碼從 Exchange 2003 服務器檢索電子郵件。 周五一切正常,現在失敗了。

通過一些調查,我將其范圍縮小到 targetDate 變量。 似乎日期是在四月,它會失敗並從服務器返回 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;
    }

終於讓它工作了。 要投射的確切目標日期(包括 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')"

請注意,秒部分是兩個零而不是三個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM