PrepareHttpWebRequestForUrl method

Future<IEwsHttpWebRequest> PrepareHttpWebRequestForUrl(
  1. Uri url,
  2. bool acceptGzipEncoding,
  3. bool allowAutoRedirect
)
Creates an HttpWebRequest instance and initializes it with the appropriate parameters, based on the configuration of this service object. The URL that the HttpWebRequest should target. If true, ask server for GZip compressed content. If true, redirection responses will be automatically followed.

Implementation

Future<IEwsHttpWebRequest> PrepareHttpWebRequestForUrl(
    Uri url, bool acceptGzipEncoding, bool allowAutoRedirect) async {
  // Verify that the protocol is something that we can handle
  // todo("add service local exception")
  if ((url.scheme != "http") && (url.scheme != "https")) {
    throw new ServiceLocalException("UnsupportedWebProtocol(${url.scheme})");
  }

  IEwsHttpWebRequest request = this
      .HttpWebRequestFactory
      .CreateRequestWithExchangeServiceAndUrl(this, url);

  request.PreAuthenticate = this.PreAuthenticate;
  request.Timeout = this.Timeout;
  this.SetContentType(request);
  request.Method = "POST";
  request.UserAgent = this.UserAgent;
  request.AllowAutoRedirect = allowAutoRedirect;
  request.CookieContainer = this.CookieContainer;
  request.KeepAlive = this._keepAlive;
  request.ConnectionGroupName = this._connectionGroupName;

  if (acceptGzipEncoding) {
    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
  }

  if (!StringUtils.IsNullOrEmpty(this._clientRequestId)) {
    request.Headers.Add("client-request-id", this._clientRequestId);
    if (this._returnClientRequestId) {
      request.Headers.Add("return-client-request-id", "true");
    }
  }

  if (this._webProxy != null) {
    request.Proxy = this._webProxy;
  }

  if (this.HttpHeaders.length > 0) {
    this
        .HttpHeaders
        .entries
        .forEach((kv) => request.Headers[kv.key] = kv.value);
  }

  request.UseDefaultCredentials = this.UseDefaultCredentials;
  if (!request.UseDefaultCredentials!) {
    ExchangeCredentials? serviceCredentials = this.Credentials;
    if (serviceCredentials == null) {
      throw new ServiceLocalException("Strings.CredentialsRequired");
    }

    // Make sure that credentials have been authenticated if required
    await serviceCredentials.PreAuthenticate();

    // Apply credentials to the request
    serviceCredentials.PrepareWebRequest(request);
  }

  this._httpResponseHeaders.clear();

  return request;
}