static void CreateAndPrepareWebRequest(HttpTransportAsyncResult self) { Trace(self, "CreateAndPrepareRequest"); var request = self.request; var settings = self.settings; var http = (HttpWebRequest)WebRequest.Create(request.Uri); http.Method = request.Method; CopySettingsToHttpWebRequest(settings, http); HttpWebRequestTransportSettings messageSettings = request.GetPropertyOrDefault <HttpWebRequestTransportSettings>(); if (messageSettings != null) { CopySettingsToHttpWebRequest(messageSettings, http); } var calc = HttpMessageCore.CalculateEffectiveContentType(request); if (calc != request.Headers.ContentType) { request.Headers.ContentType = calc; } if (!HttpContent.IsNullOrEmpty(request.Content) && request.Content.HasLength()) { if (request.Headers.ContentLength == null) { request.Headers.ContentLength = request.Content.GetLength(); } } CopyHeadersToHttpWebRequest(request.Headers, http); if (http.Method == "GET" && http.ContentLength >= 0) { throw new NotSupportedException("can't set Content-Length to " + http.ContentLength + " on " + http.Method); } if (http.Method == "GET" && !HttpContent.IsNullOrEmpty(request.Content)) { throw new NotSupportedException("can't set a non-IsEmpty content on a GET: " + self.request.Content); } self.webRequest = http; }
public static string CalculateEffectiveContentType(string header, HttpContent c) { var body = HttpContent.IsNullOrEmpty(c) ? null : c.ContentType; return(CalculateEffectiveContentType(header, body)); }
public HttpTransportAsyncResult(bool preferSync, HttpRequestMessage request, HttpWebRequestTransportSettings settings, AsyncCallback callback, object state) : base(callback, state) { if (request.Uri == null) { throw new ArgumentNullException("request", "request.Uri is null"); } if (!request.Uri.IsAbsoluteUri) { throw new UriFormatException("\"" + request.Uri + "\" is not an absolute URI"); } this.stayedSync = true; this.settings = settings; this.request = request; CancelManager.AddIfCancelManagerPresent(this.request, this); CreateAndPrepareWebRequest(this); if (!HttpContent.IsNullOrEmpty(request.Content)) { var writer = request.Content; Stream stream; if (!preferSync) { stayedSync = false; Trace(this, "Going async"); this.timedOutReason = TimeoutReason.GetRequestStream; var result = webRequest.BeginGetRequestStream(EndGetRequestStreamAndWriteCallback, this); Trace(this, "called BeginGetRequestStream"); if (result.CompletedSynchronously) { Trace(this, "BeginGetRequestStream completed synchronously"); stream = webRequest.EndGetRequestStream(result); } else { Trace(this, "went async for BeginGetRequestStream"); RegisterAbortTimeout(result, TimeoutReason.GetRequestStream); return; } } else { stream = this.webRequest.GetRequestStream(); } WriteToRequestStream(this, stream, writer); } this.timedOutReason = TimeoutReason.GetResponse; if (preferSync) { PopulateWebResponse(this, null, PopulateWebResponseSyncFunc); } else { var result = this.webRequest.BeginGetResponse(EndGetResponseCallback, this); if (result.CompletedSynchronously) { this.stayedSync = true; PopulateWebResponse(this, result, PopulateWebResponseEndSyncFunc); } else { this.stayedSync = false; RegisterAbortTimeout(result, TimeoutReason.GetResponse); } } }