Just to save time for the interests of others, I answer my question.
After several tests, the exception seems to be the same issue with HttpWebRequest , as discussed in the question. I am using Microsoft.AspNet.WebApi version 4.0.20710.0.
The following are two equivalent code snippets; the former does not work on large files, while the latter works fine.
By the way, despite the fact that the general benefits of HttpClient are becoming apparent HttpClient
using HttpClient
var clientRef = new System.Net.Http.HttpClient( new HttpClientHandler() { Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword) }); clientRef.BaseAddress = new Uri(serverAddress); clientRef.DefaultRequestHeaders.ExpectContinue = false; clientRef.PostAsync( MyFavoriteURL, new System.Net.Http.StreamContent(inputStream)).ContinueWith( requestTask => { HttpResponseMessage response = requestTask.Result; response.EnsureSuccessStatusCode(); }, TaskContinuationOptions.LongRunning).Wait();
using HttpWebRequest
// Preauthenticate var req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(MyFavoriteURL); req.Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword); req.Method = "POST"; req.PreAuthenticate = true; req.Timeout = 10000; using (var resp = (System.Net.HttpWebResponse)req.GetResponse()) { if (resp.StatusCode != System.Net.HttpStatusCode.Accepted && resp.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("Authentication error"); } } // Upload req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(MyFavoriteURL); req.Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword); req.Method = "POST"; req.PreAuthenticate = true; req.Timeout = 1200000; req.ContentLength = inputStream.Length; req.ContentType = "application/binary"; req.AllowWriteStreamBuffering = false; req.Headers.ExpectContinue = false; using (var reqStream = req.GetRequestStream()) { inputStream.CopyTo(reqStream); } using (var resp = (System.Net.HttpWebResponse)req.GetResponse()) { if (resp.StatusCode != System.Net.HttpStatusCode.Accepted && resp.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("Error uploading document"); } }
Jacek source share