我使用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());
}
}
}
虽然是Console.WriteLine(rdr.ReadLine());但是会发生什么? line会打印出状态几秒钟而不是它会停止+不再点击那里的断点,即使它仍然连接。
它应该做的是继续在控制台中写入状态,因为它是一个保持活动的连接,并且每隔几秒就会连续几次提到“Twitter”关键字。
任何可能导致此问题的想法?
答案 0 :(得分:0)
检查出来:
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();
}
}
应该让事情变得更容易。