首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpClient和PushStreamContent

问HttpClient和PushStreamContent
EN

Stack Overflow用户
提问于 2014-08-21 23:02:25
回答 2查看 10.5K关注 0票数 11

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

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

现在我的问题是:有没有可能用另一种方式来解决这个问题?从HttpClient ->通过推流上传数据到web api?

我实现了它,如下所示,但是web api并没有在客户端关闭流之前得到请求。

代码语言:javascript
复制
         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是我的带有委托的类:

代码语言:javascript
复制
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)

这对于Push-Stream是必要的。

这是不是有可能呢?直到最后一个字节被写入到流中,HttpClient才会向web api发送请求。

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

Edit:这是WriteToStream的实现(但我没有使用磁盘中的文件,而是使用了内存流'myMemoryStream‘(在构造函数中传递):

代码语言:javascript
复制
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上下文?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-29 20:31:40

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

我想设置: 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 makes HttpWebRequest/HttpClient behaviour unexpected)

对我有效的解决方案是:

代码语言:javascript
复制
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<WebResponse> response = httpWebRequest.GetResponseAsync();

         var x = response.Result;
         Stream resultStream = x.GetResponseStream();
//... read result-stream ...
票数 5
EN

Stack Overflow用户

发布于 2014-08-21 23:28:08

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25429726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档