Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# event problem...

Options
  • 27-04-2008 6:07pm
    #1
    Closed Accounts Posts: 1,444 ✭✭✭


    Hi guys, I'm trying to raise an event once the "\n" character is seen on the response stream...

    When the page www.asdf.com/test.php is accessed, it spits out the following (each line takes 5 seconds to appear)
    Hello Master 1<br>\n
    Hello Master 2<br>\n
    Hello Master 3<br>\n
    hwrq = (HttpWebRequest)WebRequest.Create("http://www.asdf.com/test.php");
    hwrs = (HttpWebResponse)hwrq.GetResponse();
    
    Stream rs = hwrs.GetResponseStream();
    StreamReader sr = new StreamReader(rs, Encoding.UTF8);
    string line;
    
    while ((line = sr.ReadLine()) != null)
    {
         label1.Text="new-line char found!";
    }
    rs.Close();
    sr.Close();
    

    Any ideas would be much appreciated. Is there such thing as a buffer event? Or an event that's raised when a certain character enters a buffer?


Comments

  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Howdy chaps,

    I think I've figured it out...

    A simple case of sending lines ending with "\r\n" instead of "\n"!

    Hello Master 1<br>\r\n
    Hello Master 2<br>\r\n
    Hello Master 3<br>\r\n

    HTTP protocol requires that lines end with a carriage return followed by a new line...

    There ya go... :(
    private void examine_stream()
    {
       try
       {
          hwrq=(HttpWebRequest)WebRequest.Create("http://www.asdf.com/test.php");
          hwrs = (HttpWebResponse)hwrq.GetResponse();
    
          Stream rs = hwrs.GetResponseStream();
          StreamReader sr = new StreamReader(rs, Encoding.UTF8);
    
          string line;
          while ((line = sr.ReadLine()) != null)
          {
             label1.text=line;
          }            
          rs.Close();
          sr.Close();
       }
       catch(etc...
    }
    

    I'd welcome any comments about this bit of code:
    while ((line = sr.ReadLine()) != null)
    {
       label1.text=line;
    }
    

    Is that while loop gonna be polling continuously? How can I make this more efficient? (btw, the function examine_stream() runs in its own thread)


Advertisement