HttpClient的和PushStreamContent [英] HttpClient and PushStreamContent

查看:1535
本文介绍了HttpClient的和PushStreamContent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PushStreamContent与我的REST API(的ASP.NET Web API)和伟大工程。
HttpClient的可以请求的ressource和之前的完整请求由服务器处理得到的HTTP响应(服务器仍然写入到推流)。

I use PushStreamContent with my REST API (ASP.NET Web API) and works great. The HttpClient can request a ressource and gets the HTTP-Response before the complete request is handled by the server (the server still writes to the push-stream).

由于HttpClient的你必须做一个小东西:使用HttpCompletionOption.ResponseHeadersRead

As HttpClient you have to do one little thing: Use HttpCompletionOption.ResponseHeadersRead.

现在我的问题:
是否有可能以这种其他的方式?
从HttpClient的? - >通过推流到Web API数据上传

Now my question: Is it possible to to this the other way? From the HttpClient -> uploading data via a push-stream to the web api?

我实现它下面,但Web API获取客户端关闭该流不前的请求。

I Implemented it as below, but the web api gets the request not before the client closes the stream.

         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是我与委托类:

The AsyncStream is my class with the delegate:

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

这是必要的推流。

这可能不知?直到最后一个字节写入流HttpClient的不发送请求到Web API ...

Is this possible somehow? The HttpClient do not send the request to the web api until the last bytes are written to the stream...

我有什么做的?在客户端或者在服务器/ asp.net网页API端?

What do I have to do? Is the problem on the client side or maybe on the server / asp.net web api-side?

编辑:
这是WriteToStream的实行(但我不使用从磁盘上的文件,是用一个MemoryStream'myMemoryStream(在构造函数中传递):

This is the implemenation of WriteToStream (but I do not use a file from disk, is use a memorystream 'myMemoryStream' (passed in the constructor):

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背景

Maybe I have to do something with: HttpContent content, TransportContext context ?

推荐答案

我找到了解决我的问题:

I found the solution to my problem:

我想设置:httpWebRequest.AllowReadStreamBuffering = FALSE;

I want to set: httpWebRequest.AllowReadStreamBuffering = false;

4.0的HttpClient默认情况下不缓冲,你不能存取权限属性AllowReadStreamBuffering,所以你必须直接使用HttpWebRequest的。 (或者你可以使用HttpClinet 4.5,也就是默认行为流式)看到:的 http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ 6 。使用的HttpClient)

HttpClient 4.0 does buffering by default and you cannot acces the property AllowReadStreamBuffering, so you have to use HttpWebRequest directly. (Or you can use HttpClinet 4.5, there is the default behaviour 'streaming') see: http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ 6. Using HttpClient)

第二个问题是小提琴手:小提琴手目前仅支持流媒体的反应,而不是请求(<一个href=\"http://stackoverflow.com/questions/13824717/fiddler-makes-httpwebrequest-httpclient-behaviour-unexpected\">Fiddler使得HttpWebRequest的/ HttpClient的行为意外)

The second problem was fiddler: Fiddler currently only supports streaming of responses and not requests (Fiddler makes HttpWebRequest/HttpClient behaviour unexpected)

这是为我工作解决办法:

The solution that worked for me:

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 ...

这篇关于HttpClient的和PushStreamContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆