use*_*899 10 c# httpclient asp.net-web-api pushstreamcontent
我将PushStreamContent与我的REST API(ASP.NET Web API)一起使用并且效果很好.HttpClient可以请求一个ressource并在服务器处理完整请求之前获取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();
Run Code Online (Sandbox Code Playgroud)
AsyncStream是我的代表类:
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
Run Code Online (Sandbox Code Playgroud)
这对于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();
}
}
Run Code Online (Sandbox Code Playgroud)
也许我必须做一些事情: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<WebResponse> response = httpWebRequest.GetResponseAsync();
var x = response.Result;
Stream resultStream = x.GetResponseStream();
//... read result-stream ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9092 次 |
| 最近记录: |