HttpWebResponse GetResponseStream 挂在 Dispose 中

发布于 2024-11-04 11:22:33 字数 857 浏览 1 评论 0原文

我正在使用 HttpWebResponse.GetResponseStream 访问互联网广播流,并希望从响应流中读取一些数据,然后断开连接。但是,我总是无限期地挂在流的 Dispose 上。下面的单元测试将显示“清理网络流...”,但永远不会显示“完成”。这是为什么呢?我是否应该通过不去处理我的网络流来解决这个问题?

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    int total = 0;
    byte[] buffer = new byte[1024];

    using(var networkStream = resp.GetResponseStream())
    {
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up networkStream...");
    }
    Console.WriteLine("Finished");
}

I am using HttpWebResponse.GetResponseStream to access an internet radio stream, and want to read some data from the response stream, and then disconnect. However, I always hang indefinitely on the Dispose of the stream. The unit test below will display "Cleaning up networkStream...", but never get to "Finished". Why is this? And should I fix it by just not bothering to Dispose of my networkStream?

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    int total = 0;
    byte[] buffer = new byte[1024];

    using(var networkStream = resp.GetResponseStream())
    {
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up networkStream...");
    }
    Console.WriteLine("Finished");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2024-11-11 11:22:33

编辑:刚刚找到了一个解决方案...在 using 块结束之前调用 req.Abort() 。不是很优雅,但它可以工作......所以代码变成:

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    {
        int total = 0;
        byte[] buffer = new byte[1024];

        var networkStream = resp.GetResponseStream();
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up HttpWebResponse...");
        req.Abort();
    }
    Console.WriteLine("Finished");
}

尝试处理 HttpWebResponse 相反:

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    {
        int total = 0;
        byte[] buffer = new byte[1024];

        var networkStream = resp.GetResponseStream();
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up HttpWebResponse...");
    }
    Console.WriteLine("Finished");
}

它应该处理 NetworkStream也是如此。

EDIT: just found a solution... call req.Abort() before the end of the using block. Not very elegant, but it works... So the code becomes:

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    {
        int total = 0;
        byte[] buffer = new byte[1024];

        var networkStream = resp.GetResponseStream();
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up HttpWebResponse...");
        req.Abort();
    }
    Console.WriteLine("Finished");
}

Try disposing the HttpWebResponse instead:

[Test]
public void CanStreamMP3Radio()
{
    string url = @"http://radio.reaper.fm/stream/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    {
        int total = 0;
        byte[] buffer = new byte[1024];

        var networkStream = resp.GetResponseStream();
        do
        {
            int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
            Console.WriteLine("{0} bytesRead", bytesRead);
            total += bytesRead;
        } while (total < 16384);
        Console.WriteLine("Cleaning up HttpWebResponse...");
    }
    Console.WriteLine("Finished");
}

It should dispose the NetworkStream as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文