6

以下函数将从 Twitter firehose 中拉取前 X 条消息,但看起来像 WebResponse 块并且永远不会退出该函数:

public void GetStatusesFromStream(string username, string password, int nMessageCount)
{
    WebRequest request = WebRequest.Create("http://stream.twitter.com/1/statuses/sample.json");
    request.Credentials = new NetworkCredential(username, password);

    using (WebResponse response = request.GetResponse())
    {
        using (var stream = response.GetResponseStream())
        {
            using (var reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    Console.WriteLine(reader.ReadLine());

                    if (nMessageCount-- < 0)
                        break;
                }
                Console.WriteLine("Start iDispose");
            }
            Console.WriteLine("Never gets here!!!");
        }
    }

   Console.WriteLine("Done - press a key to exit");
   Console.ReadLine();
}

但以下工作正常:

public void GetStatusesFromStreamOK(string username, string password, int nMessageCount)
    {
    byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);
    //request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(encbuff));

    string requestString = "GET /1/statuses/sample.json HTTP/1.1\r\n";
    requestString += "Authorization: " + "Basic " + Convert.ToBase64String(encbuff) + "\r\n";
    requestString += "Host: stream.twitter.com\r\n";
    requestString += "Connection: keep-alive\r\n";
    requestString += "\r\n";

    using (TcpClient client = new TcpClient())
    {
        client.Connect("stream.twitter.com", 80);

        using (NetworkStream stream = client.GetStream())
        {
            // Send the request.
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(requestString);
            writer.Flush();

            // Process the response.
            StreamReader rdr = new StreamReader(stream);

            while (!rdr.EndOfStream)
            {
                Console.WriteLine(rdr.ReadLine());
                if (nMessageCount-- < 0)
                    break;
            }

        }
    }

   Console.WriteLine("Done - press a key to exit");
   Console.ReadLine();

}

我究竟做错了什么?

4

2 回答 2

3

将您的WebRequest作为HttpWebRequest, 然后在休息电话之前request.Abort()

于 2009-12-14T19:47:54.883 回答
-1

看起来它与处置过程或“使用”有关......

以下代码工作正常(没有“使用”语句):

public static void GetStatusesFromStream(string username, string password, int nMessageCount)
    {
        WebRequest request = WebRequest.Create("http://stream.twitter.com/1/statuses/sample.json");
        request.Credentials = new NetworkCredential(username, password);

        WebResponse response = request.GetResponse();
        {
            var stream = response.GetResponseStream();
            {
                var reader = new StreamReader(stream);
                {
                    while (!reader.EndOfStream)
                    {
                        Console.WriteLine(reader.ReadLine());

                        if (nMessageCount-- < 0)
                            break;
                    }
                }
                Console.WriteLine("Never gets here!!!");
            }
        }

        Console.WriteLine("Done - press a key to exit");
        Console.ReadLine();
    }
于 2009-12-11T21:18:41.320 回答