Resolved speed up HttpWebRequest

Snoopy

New member
Joined
Jun 18, 2020
Messages
3
Programming Experience
Beginner
hi all since i am new here i hope this is the right place.
i am trying out HttpWebRequest to check out the status code (in this case response) from different websites.
On ok the response is very fast and no form freeze.
On a connect failure the form freezes and take's around 5 seconds
bypassing the freezing can be done with a background worker.
But is there a way to say, there is no response in 1 second, forget this one and go to the next.

C#:
 try
            {

                string url;
                url = (LoopUrlFromListboxOrStream ) ; //example

                // Creates an HttpWebRequest for the specified URL.
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                // Sends the HttpWebRequest and waits for a response.
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    label1.Text = (myHttpWebResponse.StatusDescription);
                // Releases the resources of the response.
                myHttpWebResponse.Close();
            }
            catch (WebException ex)
            {
                label2.Text = (ex.Status.ToString());
            }
            catch (Exception ex)
            {
                label2.Text = (ex.Message.ToString());
            }

thx
 
By the way, the HttpWebRequest supports asynchronous methods (the old kind; not async/await) so you don't have to do everything on the UI thread or use a dedicated background thread. You might also consider using a WebClient or HttpClient, which are simpler to use if you don't need the fine-grained control.
 
Back
Top Bottom