views:

55

answers:

1

I have the following code using the TcpClient

byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("username" + ":" + "password");
        string follow = "track=soccer,twitter";
        byte[] encode_follow = System.Text.Encoding.UTF8.GetBytes(follow);

        using (TcpClient client = new TcpClient())
        {
            string requestString = "POST /1/statuses/filter.json HTTP/1.1\r\n";
            requestString += "Host: stream.twitter.com\r\n";
            requestString += "User-Agent: www.site.com\r\n";
            requestString += "Referer: http://www.site.com\r\n";
            requestString += "Content-Type: application/x-www-form-urlencoded\r\n";
            requestString += "Authorization: " + "Basic " + Convert.ToBase64String(encbuff) + "\r\n";
            requestString += "Content-length: " + follow.Length + "\r\n";
            requestString += "Connection: keep-alive\r\n";
            requestString += "\r\n";
            requestString += follow;
            requestString += "\r\n\r\n";

            client.Connect("stream.twitter.com", 80);
            //client.ReceiveTimeout = 100;
            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());
                        Console.WriteLine("IS end: " + rdr.EndOfStream.ToString());
                    }
                }

        }

What happens though is that the Console.WriteLine(rdr.ReadLine()); line will print out statuses for a few seconds than it would stop + not hit the break point there anymore, even though it's still connected.

What it should be doing is continuing to write the statuses in the console, since it's a keep-alive connection and the 'Twitter' keyword gets mentioned a few times every few seconds continuously.

Any ideas what could be causing this issue?

A: 

Check this out:

public class Program
{
    public static void Main()
    {
        using (var client = new WebClient())
        {
            client.Credentials = new NetworkCredential("username", "password");
            client.OpenReadCompleted += (sender, e) =>
            {
                using (var reader = new StreamReader(e.Result))
                {
                    while (!reader.EndOfStream)
                    {
                        // TODO: feed this to your favorite JSON parser
                        Console.WriteLine(reader.ReadLine());
                    }
                }
            };
            client.OpenReadAsync(new Uri("http://stream.twitter.com/1/statuses/filter.json?track=soccer,twitter"));
        }
        Console.ReadLine();
    }
}

Should make things easier.

Darin Dimitrov
Thanks, your code actually had the same problem - which is how I figured out what's wrong. Turns out it was a problem with the streaming permissions for one account. I tried with another account and it was fine.
rksprst