HttpWebRequest Causing 100% cpu

acadenilla
acadenilla used Ask the Experts™
on
I have a windows service that calls a 3rd party RESTFul services. I am using HttpWebRequest to make the request and get the XML that is returned.

Sometimes the CPU is at 100% and i was wondering what could be causing this.

int timeoutInterval = ((int)ConfigurationManager.GetAppConfigValueOrDefault<double>("TMGGetDataTimeoutInSeconds", 10.0)) * 1000;
                
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                if (request != null)
                {
                    request.Timeout = timeoutInterval;
                    request.ReadWriteTimeout = timeoutInterval;
                    request.KeepAlive = false;
                    request.Proxy = WebRequest.DefaultWebProxy;

                    ServicePointManager.UseNagleAlgorithm = true;
                    ServicePointManager.Expect100Continue = true;
                    ServicePointManager.CheckCertificateRevocationList = true;
                    ServicePointManager.DefaultConnectionLimit = 200;

                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                            {
                                responseStr = responseStream.ReadToEnd();
                                return responseStr;
                            }
                        }
                    }
                }

                return responseStr;

Open in new window


Thanks

Allan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can use this:-

class HttpRequestProcessor
    {
        private HttpWebRequest _request;

        public HttpRequestProcessor(string url)
        {
            _request = WebRequest.Create(url) as HttpWebRequest;
        }

        public void Process()
        {
            _request.BeginGetResponse(OnProcessComplete, _request);
        }

        private void OnProcessComplete(IAsyncResult result)
        {
            var req = (HttpWebRequest) result.AsyncState;
            var response = req.EndGetResponse() as HttpWebResponse;

            if(response!=null && response.StatusCode==HttpStatusCode.OK)
            {
                
            }
        }
    }

Open in new window

Here is how to call this:-
var request = new HttpRequestProcessor("http://www.google.com");
request.Process();

Open in new window


This will use the thread pool thread to process the request...

Author

Commented:
Thanks BuggyCoder,

What would be the advantage of using a Async call?
well by the time your call is in progress, your cpu can be shared for other resources.
As soon as your call returns, you will be notified in OnProcessCompleted method.

Internally, Asyn Calls use ThreadPool Threads, where a thread rests/sleeps after making the call, thus not hogging up the CPU till the time the request is going on as in case of non-async call.

Read Here For More Detail:-
http://www.codeproject.com/Articles/34536/Asynchronous-Input-Output-and-NET-s-Asynchronous-P

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial