locked
How to achieve long polling to url using .net 3.5 framework RRS feed

  • Question

  • Hi sir,

    Please hep me to make log polling to particular URL.

    Eg: Url = http://<ip>/testsample/main.php

    for the above url i have to make a http request. This url response returns the interval value.

    Based on the interval again has to make a http request to same url. It keeps making the request.

    Please help me with snippet of code to do this.

    thanks in advance,

    Ravi



    • Edited by RaviRaju Friday, December 7, 2018 2:38 PM
    Friday, December 7, 2018 2:37 PM

Answers

  • Hi RaviRaju,

    Is there any update? Do you resolve the issue, I think that CoolDadTx provide a good idea and code. If it is a standalone application which does not have other function, the following code for your reference.

    while (true)
                {
                    HttpWebRequest request = WebRequest.Create("http://<ip>/testsample/main.php") as HttpWebRequest;
    
                    //request.Accept = "application/xrds+xml";  
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    WebHeaderCollection header = response.Headers;
    
                    var encoding = ASCIIEncoding.ASCII;
                    using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                    {
                        string responseText = reader.ReadToEnd();
                        //you could check the result to do something else.
                    }
                    //  interval value.
                    Thread.Sleep(2000);
                }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 10, 2018 5:09 AM

All replies

  • There is insufficient information to provide you an example as different URLs use different content types (JSON, form, XML), frameworks (WCF, REST) and return different data (JSON, XML) . You have to already know all this before you can make a call. The algorithm itself is straightforward so I'll discuss that. You'll need to put in your URL-specific logic though.

    Use HttpClient or WebClient (based upon need) to query the URL and get the data back in the format that you need (JSON, XML, etc). Samples on how to do that are in the links. Getting the actual data from the response is very much API specific so we cannot tell you how to do that other than try to use JSON and then use JsonConvert or similar JSON library to get the response into a strongly typed object.

    Once you have the interval in a .NET variable then you can wait. If you want to block the current thread (probably not a good idea unless it is a background thread) then use Thread.Sleep but most likely you'll want to use Task.Delay (if in an async call) or a Timer to schedule the next call. When the thread/task/timer starts running again repeat the code you just wrote. Assuming all this code is in a method then it should be pretty straightforward because you're just continually running the same code over and over again.

    //Startup code - using System.Timers.Timer
    _timer = new Timer(1000) { AutoReset = false };
    _timer.Elapsed += Refresh;
    
    //Timer approach
    void Refresh ( object sender, ElapsedEventArgs e )
    {
       var client = new WebClient();
       //Get data from URL
       var interval = …;
    
       //Adjust the interval and wait some more
       _timer.Interval = interval;
       _timer.Start();
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 7, 2018 8:39 PM
  • Hi RaviRaju,

    Is there any update? Do you resolve the issue, I think that CoolDadTx provide a good idea and code. If it is a standalone application which does not have other function, the following code for your reference.

    while (true)
                {
                    HttpWebRequest request = WebRequest.Create("http://<ip>/testsample/main.php") as HttpWebRequest;
    
                    //request.Accept = "application/xrds+xml";  
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    WebHeaderCollection header = response.Headers;
    
                    var encoding = ASCIIEncoding.ASCII;
                    using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                    {
                        string responseText = reader.ReadToEnd();
                        //you could check the result to do something else.
                    }
                    //  interval value.
                    Thread.Sleep(2000);
                }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 10, 2018 5:09 AM
  • Thank your so much for your help.

    I will check and updated you.

    Thursday, December 13, 2018 7:11 AM
  • Hi RaviRaju,

    Is there any update? do you resolve the issue. if you have any issue, please feel free let me know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 21, 2018 3:16 AM
  • HI Wenday Zang,

    Thank you so much for your help it helped me.

    Regards,

    Ravi.

    Friday, February 15, 2019 10:15 AM