Reading data from an open HTTP stream

前端 未结 4 666
醉梦人生
醉梦人生 2021-02-02 16:23

I am trying to use the .NET WebRequest/WebResponse classes to access the Twitter streaming API here \"http://stream.twitter.com/spritzer.json\".

I need to b

4条回答
  •  耶瑟儿~
    2021-02-02 16:58

    I ended up using a TcpClient, which works fine. Would still be interested to know if this is possible with WebRequest/WebResponse though. Here is my code in case anybody is interested:

    using (TcpClient client = new TcpClient())
    {
    
        string requestString = "GET /spritzer.json HTTP/1.1\r\n";
        requestString += "Authorization: " + token + "\r\n";
        requestString += "Host: stream.twitter.com\r\n";
        requestString += "Connection: keep-alive\r\n";
        requestString += "\r\n";
    
        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());
            }
        }
    }
    

提交回复
热议问题