WP8.1 HttpClient Stream仅获得65536字节数据 [英] WP8.1 HttpClient Stream got only 65536 bytes data

查看:119
本文介绍了WP8.1 HttpClient Stream仅获得65536字节数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows运行时上为win8.1和wp8.1的MediaElement编写实时flv流多路分配器.

I am trying write a real-time flv stream demuxer on windows-runtime, for win8.1 and wp8.1's MediaElement.

我已经完成了多路分解代码,可以将flv文件正确地多路分解为h264和aac标签数据.

I've already finish the demux code, flv files can be correctly demuxed into h264 and aac tag-datas.

当我尝试播放网络文件和流时,遇到了一个非常奇怪的网络问题:

When I was trying to play network files and streams, I got a very strange network problem:

相同的代码,

  1. 在win8.1下运行,一切正常,无论文件还是网络流(证明demux代码都可以);
  2. 在wp8.1(真实电话或仿真器)下运行,文件良好,网络流不良-不管我如何从HttpClient Stream读取字节,目标服务器仅给我65536字节数据,然后连接被阻塞,没有任何响应和错误,甚至没有超时,这只是阻塞线程.

打开流的代码:

var uri = new Uri("http://hdl.xxx.com/live/yyyy")
//uri is dymatic
var client = new HttpClient();
var stream = await client.GetStreamAsync(uri);
openStream(stream)

用于读取数据的代码:

public static byte[] ReadBlocks(this Stream stream, int count)
{
    byte[] buffer = new byte[count];
    int offset = 0;
    int length;

    while (offset < count)
    {
        //a loop statement to guarantee I can get *count* bytes
        Debug.WriteLine("read " + (count - offset));
        //a debug message show how many bytes do I need
        length = stream.Read(buffer, offset, count - offset);
        if (length == 0)
        {
            throw new EndOfStreamException();
        }
        Debug.WriteLine("got " + length);
        //a debug message show how many bytes I got
        offset += length;
    }
    return buffer;
}

例如,当我需要从flv流中获取1024个字节时,我在wp8.1下运行stream.ReadBlocks(1024),调试将告诉我:

For example, when I need to rea 1024 bytes from the flv stream, I run stream.ReadBlocks(1024) under wp8.1, the debug tells me like:

read 1024
got 768
read 256

,然后什么也没有发生.我写了一个额外的计数器,该计数器显示一旦服务器发送总计65536字节,则流的下一个Read方法将始终被阻塞.

and then nothing happend any more. I wrote an extra counter, the counter shows once server send a total of 65536 bytes, next Read method of stream will always be choked.

我确定uri可用.我可以使用PC Web浏览器将一些流数据下载为flv文件,并且此下载的flv文件也可以在wp8.1下播放.

I'm sure the uri is available. I can download some stream data as a flv file by using pc web browser, and this downloaded flv file can be played under wp8.1 as well.

似乎此问题仅在wp8.1下才会发生,android和ios均不受影响.

It looks like this problem only happens under wp8.1, android and ios are not affected.

那是我的代码问题还是服务器设置不正确?

So is it my code's problem or actually the server is not set up properly?

从过去的三周开始,我尝试了所有可以打开流的http方法,但仍然阻塞了65536个字节.

From last three weeks, I've tried every http method that can open a stream, but still got choked at 65536 bytes.

有人可以帮我吗?

推荐答案

我刚刚解决了相同的问题-不要使用System.Net.HttpClient,而要使用Windows.Web.Http.HttpClient

I just solved the same problem - do NOT use System.Net.HttpClient, but Windows.Web.Http.HttpClient

System.Net中的一个使用头连接:默认情况下关闭,这将导致流关闭,仅读取65 kB.它还包含一个错误,该错误阻止您将标头覆盖为Keep-Alive(抛出一些无意义的异常)

The one in System.Net uses header Connection: Close by default, which causes the stream to close, reading only 65 kB. It also contains a bug which prevents you to override the header to Keep-Alive (it throws some nonesense exception)

这篇关于WP8.1 HttpClient Stream仅获得65536字节数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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