HttpClient和PushStreamContent

我使用PushStreamContent和我的REST API(ASP.NET Web API)并且效果很好。 HttpClient可以请求资源并在服务器处理完整请求之前获取HTTP响应(服务器仍然写入推送流)。

作为HttpClient,你必须做一件小事:使用HttpCompletionOption.ResponseHeadersRead。

现在我的问题:是否有可能以另一种方式? 从HttpClient – >通过push-stream将数据上传到web api?

我在下面实现了它,但是web api在客户端关闭流之前没有收到请求。

var asyncStream = new AsyncStream(fs); PushStreamContent streamContent = new PushStreamContent(asyncStream.WriteToStream); content.Add(streamContent); HttpResponseMessage response = await c.SendAsync(new HttpRequestMessage(new HttpMethod("POST"), "http://localhost/...") { Content = content }, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); 

AsyncStream是我的代表类:

 public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context) 

这对于Push-Stream是必要的。

这有可能吗? 在将最后一个字节写入流之前,HttpClient不会将请求发送到Web api …

我需要做什么? 问题出在客户端还是在服务器/ asp.net web api端?

编辑:这是WriteToStream的实现(但我不使用磁盘中的文件,使用内存流’myMemoryStream’(在构造函数中传递):

 public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context) { try { var buffer = new byte[4096]; using (var stream = myMemoryStream) { var bytesRead = 1; while (bytesRead > 0) { bytesRead = video.Read(buffer, 0, buffer.Length); outputStream.Write(buffer, 0, bytesRead); } } } catch (HttpException ex) { return; } finally { outputStream.Close(); } } 

也许我必须做一些事情:HttpContent内容,TransportContext上下文?

我找到了解决问题的方法:

我想设置:httpWebRequest.AllowReadStreamBuffering = false;

HttpClient 4.0默认执行缓冲,您无法访问属性AllowReadStreamBuffering,因此您必须直接使用HttpWebRequest。 (或者你可以使用HttpClinet 4.5,默认行为’流”): http : //www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api / 6.使用HttpClient)

第二个问题是fiddler:Fiddler目前只支持响应流而不是请求( Fiddler使HttpWebRequest / HttpClient行为意外 )

对我有用的解决方案:

 HttpWebRequest httpWebRequest = HttpWebRequest.Create(...) httpWebRequest.Method = "POST"; httpWebRequest.Headers["Authorization"] = "Basic " + ...; httpWebRequest.PreAuthenticate = true; httpWebRequest.AllowWriteStreamBuffering = false; httpWebRequest.AllowReadStreamBuffering = false; httpWebRequest.ContentType = "application/octet-stream"; Stream st = httpWebRequest.GetRequestStream(); st.Write(b, 0, b.Length); st.Write(b, 0, b.Length); //... Task response = httpWebRequest.GetResponseAsync(); var x = response.Result; Stream resultStream = x.GetResponseStream(); //... read result-stream ... 

确保执行requestMessage.Headers.TransferEncodingChunked = true; 在你的请求消息…原因是如果你没有设置这个,HttpClient会缓冲客户端本身的整个内容,以便弄清楚请求的内容长度,这就是你注意到你的web api的原因在写入流时不立即调用服务…