我正嘗試在我的c#應用程序中設置EWS的推送通知。如何向EWS發送OK響應
從服務器獲得響應並使用NetworkStream讀取它之後,我需要在SOAP消息中用Ok對服務器做出響應。我能找到的唯一例子是使用Microsoft.Web.Services3和SoapEnvelope。我的理解是,這已經被WCF所取代,我真的很想使用更新的技術(學習它們)。
我該如何去發送一個SOAP消息回服務器,大概是使用與我得到通知的NetworkStream相同的?
下面是我嘗試過的一些代碼,但由於某些原因它失敗了。
const string RESPONSE_OK = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\"><soap:Body>" +
"<SendNotificationResult xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">" +
"<SubscriptionStatus>OK</SubscriptionStatus></SendNotificationResult></soap:Body></soap:Envelope>";
responseBytes = encoding.GetBytes(RESPONSE_OK);
// Send the result
HTTPResponseStruct _httpResponse;
_httpResponse.version = "HTTP/1.1";
_httpResponse.BodyData = responseBytes;
_httpResponse.Headers = new Hashtable();
_httpResponse.Headers.Add("Server", "IT12");
_httpResponse.Headers.Add("Date", DateTime.Now.ToString("r"));
_httpResponse.Headers.Add("Content-Type", "text/xml; charset=utf-8");
_httpResponse.Headers.Add("Content-Length", _httpResponse.BodyData.Length);
_httpResponse.Headers.Add("Connection", "close");
string HeadersString = _httpResponse.version + " "
+ "200 OK" + "\r\n";
foreach (DictionaryEntry Header in _httpResponse.Headers)
{
HeadersString += Header.Key + ": " + Header.Value + "\r\n";
}
HeadersString += "\r\n";
byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString);
// Send headers
clientStream.Write(bHeadersString, 0, bHeadersString.Length);
// Send body
if (_httpResponse.BodyData != null)
clientStream.Write(_httpResponse.BodyData, 0,
_httpResponse.BodyData.Length);
// clientStream.Write(responseBytes, 0, responseBytes.Length);
clientStream.Flush();
感謝, 彼得
你忘記提及它失敗的原因。包含異常類型,錯誤消息或描述意外行爲。 – Timwi 2010-08-01 07:33:36
EWS通知我一個變化,然後我應該讓它知道我收到了通知。如果不是,那麼它會在30分鐘內再試一次,然後繼續加倍,直到達到我訂閱(5分鐘)中指定的時間,然後自動取消訂閱。有沒有例外,只有幾個通知,直到它停止。 – Pieter 2010-08-01 18:05:21
你是否設法讓它工作? – 2010-12-20 12:06:48