-->

On WP8.1 ONLY: cannot read more than 65536 bytes o

2019-09-05 14:52发布

问题:

Hope someone could please provide any help on an issue I'm struggling for quite a lot of time.

Goal: I need to read an http stream from a specified URI, that I startup & endlessly read with the following code (which I stripped down to minimum so to really focus on the bare communication problem):

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();
    }
}

Issue: it happens that the above same exact code perfectly runs on a demo desktop WPF app, reading "gigabytes" of data without any issue, whereas on a Windows Phone 8.1 Store App I can only read up to 65536 bytes, and then the subsequent call to s.Read(buffer, 0, buffer.Length) (in the infinite while loop) just hangs forever, without any exception!

Things I have already tried, without any result:

  • Changing several values for the buffer size (i.e. 256, 512, 1024… and so on)
  • Running the WP 8.1 app on both device and emulator
  • Sniffing traffic with WireShark, I can see the startup request is exactly the same on both WPF and WP 8.1 scenarios, and both are HTTP 1.1, and in all cases, the server (a D-link DCS-920 webcam) continues to flawlessly “pump” HTTP 1.1 responses (mjpeg data) into the stream

Does anybody have an idea of what could be going on with this bare, simple HTTP Stream usage on WP 8.1? How could there be a 65536-byte limitation on reads?

Thanks for any help!

回答1:

This is how I am downloading an images from the web. The file size in the example is 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();