【问题标题】:On WP8.1 ONLY: cannot read more than 65536 bytes off an HTTP Stream仅在 WP8.1 上:无法从 HTTP 流中读取超过 65536 个字节
【发布时间】:2023-04-09 00:23:01
【问题描述】:

希望有人可以就我一直在努力解决的问题提供任何帮助。

目标:我需要从指定的 URI 读取 http 流,我启动并使用以下代码无休止地读取该流(我将其精简到最低限度,以便真正专注于裸露的通信问题):

public void StartupStream(Uri uri)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

    // Start the asynchronous request
    request.BeginGetResponse(OnGetResponse, request);
}
private void OnGetResponse(IAsyncResult asyncResult)
{
    // get the response
    HttpWebRequest req = (HttpWebRequest)asyncResult.AsyncState;
    try
    {
        using (HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(asyncResult))
        {
            using (Stream s = resp.GetResponseStream())
            {
                // dummy-read the stream forever
                int readBytes = 0;
                byte[] buffer = new byte[4096];
                while (true)
                {
                    readBytes += s.Read(buffer, 0, buffer.Length);
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        req.Abort();
    }
}

问题:碰巧上面完全相同的代码在演示桌面 WPF 应用程序上完美运行,读取“千兆字节”数据没有任何问题,而在 Windows Phone 8.1 Store 应用程序上我只能最多读取 65536 个字节,然后对 s.Read(buffer, 0, buffer.Length) 的后续调用(在无限 while 循环中)永远挂起,无一例外!

我已经尝试过的事情,没有任何结果:

  • 更改缓冲区大小的多个值(即 256、512、1024... 和 等等)
  • 在设备和模拟器上运行 WP 8.1 应用程序
  • 用WireShark嗅探流量,我可以看到启动请求在WPF和WP 8.1两种场景下都是一样的,都是HTTP 1.1,在所有情况下,服务器(D-link DCS-920 网络摄像头)继续完美地将 HTTP 1.1 响应(mjpeg 数据)“泵入”流中

有人知道在 WP 8.1 上使用这种简单的 HTTP 流会发生什么吗?怎么会有 65536 字节的读取限制?

感谢您的帮助!

【问题讨论】:

    标签: wpf windows windows-phone-8.1 httpwebrequest http-streaming


    【解决方案1】:

    这就是我从网上下载图像的方式。示例中的文件大小为 1131683

         string URL = "http://img.uuhy.com/uploads/2010/05/4423_Free-high-resolution-desktop-wallpaper-8.jpg"; 
            var httpClient = new HttpClient();
            var httpResponse = await httpClient.GetAsync(URL);
    
           var ImageArray = await httpResponse.Content.ReadAsByteArrayAsync();
    

    【讨论】:

    • 谢谢 Stuart,事实上 HttpClient 似乎不受 65536 字节的限制。赞!只是想知道为什么更“标准”的方法 HttpWebResponse 会严重阻塞@ 65536 字节......并且仅在 WP 上......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2011-05-15
    • 2020-09-22
    • 2013-04-12
    相关资源
    最近更新 更多