private static HttpWebRequest CreateWebRequest()

in src/Elastic.Transport/Components/TransportClient/HttpWebRequestInvoker.cs [325:381]


	private static HttpWebRequest CreateWebRequest(Endpoint endpoint, BoundConfiguration boundConfiguration, PostData? postData, bool isAsync)
	{
		var request = (HttpWebRequest)WebRequest.Create(endpoint.Uri);

		request.Accept = boundConfiguration.Accept;
		request.ContentType = boundConfiguration.ContentType;
#if NETFRAMEWORK
		// on netstandard/netcoreapp2.0 this throws argument exception
		request.MaximumResponseHeadersLength = -1;
#endif
		request.Pipelined = boundConfiguration.HttpPipeliningEnabled;

		if (boundConfiguration.TransferEncodingChunked)
			request.SendChunked = true;

		if (boundConfiguration.HttpCompression)
		{
			request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
			request.Headers.Add("Accept-Encoding", "gzip,deflate");
			request.Headers.Add("Content-Encoding", "gzip");
		}

		var userAgent = boundConfiguration.UserAgent?.ToString();
		if (!string.IsNullOrWhiteSpace(userAgent))
			request.UserAgent = userAgent;

		if (!string.IsNullOrWhiteSpace(boundConfiguration.RunAs))
			request.Headers.Add(BoundConfiguration.RunAsSecurityHeader, boundConfiguration.RunAs);

		if (boundConfiguration.Headers != null && boundConfiguration.Headers.HasKeys())
			request.Headers.Add(boundConfiguration.Headers);

		if (boundConfiguration.MetaHeaderProvider is not null)
		{
			foreach (var producer in boundConfiguration.MetaHeaderProvider.Producers)
			{
				var value = producer.ProduceHeaderValue(boundConfiguration, isAsync);

				if (!string.IsNullOrEmpty(value))
					request.Headers.Add(producer.HeaderName, value);
			}
		}

		var timeout = (int)boundConfiguration.RequestTimeout.TotalMilliseconds;
		request.Timeout = timeout;
		request.ReadWriteTimeout = timeout;

		//WebRequest won't send Content-Length: 0 for empty bodies
		//which goes against RFC's and might break i.e IIS when used as a proxy.
		//see: https://github.com/elastic/elasticsearch-net/issues/562
		var m = endpoint.Method.GetStringValue();
		request.Method = m;
		if (m != "HEAD" && m != "GET" && postData == null)
			request.ContentLength = 0;

		return request;
	}