private HttpWebResponse SendMAPIHttpRequest(string userName, string password, IRequestBody requestBody, ServerEndpoint endpoint, CookieCollection cookies, WebHeaderCollection resquestHeaders, out byte[] rawBuffer)
{
rawBuffer = null;
HttpWebResponse response = null;
AdapterHelper.Counter++;
string url = string.Empty;
if (endpoint == ServerEndpoint.MailboxServerEndpoint)
{
if (string.IsNullOrEmpty(this.mailStoreUrl))
{
this.GetEndpointUrl();
}
url = this.mailStoreUrl;
}
else
{
if (string.IsNullOrEmpty(this.addressBookUrl))
{
this.GetEndpointUrl();
}
url = this.addressBookUrl;
}
System.Net.ServicePointManager.ServerCertificateValidationCallback =
new System.Net.Security.RemoteCertificateValidationCallback(Common.ValidateServerCertificate);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = "application/mapi-http";
request.Credentials = new NetworkCredential(userName, password, this.domainName);
request.Headers.Add(resquestHeaders);
request.CookieContainer.Add(cookies);
request.Timeout = System.Threading.Timeout.Infinite;
byte[] buffer = null;
if (requestBody != null)
{
buffer = requestBody.Serialize();
request.ContentLength = buffer.Length;
}
else
{
request.ContentLength = 0;
}
if (requestBody != null)
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
}
try
{
response = request.GetResponse() as HttpWebResponse;
// Read the HTTP response buffer and parse the response to correct format.
if (response.StatusCode == HttpStatusCode.OK)
{
rawBuffer = this.ReadHttpResponse(response);
}
}
catch (WebException ex)
{
this.Site.Log.Add(
LogEntryKind.Comment,
"A WebException happened when connecting the server, The exception is {0}.",
ex.Message);
return (HttpWebResponse)ex.Response;
}
return response;
}