void RequestCompleted(QueueRequest request, string responseString)
 {
     if (request.Callback != null)
     {
         request.Callback(new WebRequestCompletedArgs(responseString));
     }
 }
        void MakeRequest()
        {
            if (currentStatus == QueueStatus.Stopped)
                return;

            currentRequest = requests.First();

            try
            {
                WebRequest = (HttpWebRequest)HttpWebRequest.Create(currentRequest.Url);
                if (IsolatedStorageSettings.ApplicationSettings.Contains("RemoteServerUsername") &&
                    (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerUsername"] != "" &&
                    IsolatedStorageSettings.ApplicationSettings.Contains("RemoteServerPassword") &&
                    (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerPassword"] != "")
                {
                    WebRequest.Credentials = new NetworkCredential((string)IsolatedStorageSettings.ApplicationSettings["RemoteServerUsername"], (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerPassword"]);
                }
                WebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), this);
            }
            catch (WebException e)
            {
                WebRequest = null;
                if (currentRequest.Callback != null)
                {
                    currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.WebException));
                }
            }
            catch
            {
                WebRequest = null;
                if (currentRequest.Callback != null)
                {
                    currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.BeginWebRequestGeneralError));
                }
            }
            //
            if (WebRequest != null)
            {
                requests.RemoveAt(0);
            }
        }
 void RequestCompleted(QueueRequest request, string responseString)
 {
     if (request.Callback != null)
     {
         request.Callback(new WebRequestCompletedArgs(responseString));
     }
 }
        void MakeRequest()
        {
            if (currentStatus == QueueStatus.Stopped)
            {
                return;
            }

            currentRequest = requests.First();

            try
            {
                WebRequest = (HttpWebRequest)HttpWebRequest.Create(currentRequest.Url);
                if (IsolatedStorageSettings.ApplicationSettings.Contains("RemoteServerUsername") &&
                    (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerUsername"] != "" &&
                    IsolatedStorageSettings.ApplicationSettings.Contains("RemoteServerPassword") &&
                    (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerPassword"] != "")
                {
                    WebRequest.Credentials = new NetworkCredential((string)IsolatedStorageSettings.ApplicationSettings["RemoteServerUsername"], (string)IsolatedStorageSettings.ApplicationSettings["RemoteServerPassword"]);
                }
                WebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), this);
            }
            catch (WebException e)
            {
                WebRequest = null;
                if (currentRequest.Callback != null)
                {
                    currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.WebException));
                }
            }
            catch
            {
                WebRequest = null;
                if (currentRequest.Callback != null)
                {
                    currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.BeginWebRequestGeneralError));
                }
            }
            //
            if (WebRequest != null)
            {
                requests.RemoveAt(0);
            }
        }
        void FinishWebRequest(IAsyncResult result)
        {
            HTTPRequestQueue ptr = (HTTPRequestQueue)result.AsyncState;

            if (currentStatus == QueueStatus.Stopped)
            {
                ptr.WebRequest = null; //Queue is been stopped
                return;
            }

            HttpWebRequest   request        = (HttpWebRequest)ptr.WebRequest;
            HttpWebResponse  response       = null;
            Stream           streamResponse = null;
            StreamReader     streamRead     = null;
            WebRequestStatus status         = WebRequestStatus.Completed;

            bool requestSuccess = true;

            try
            {
                // End the operation
                response       = (HttpWebResponse)request.EndGetResponse(result);
                streamResponse = response.GetResponseStream();
                streamRead     = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();
                RequestCompleted(currentRequest, responseString); //Request completed succesfully
            }
            catch (WebException e)
            {
                requestSuccess = false;
                status         = WebRequestStatus.WebException;
            }
            catch (Exception ex)
            {
                requestSuccess = false;
                status         = WebRequestStatus.FinishWebRequestGeneralError;
            }
            finally
            {
                // Close stream objects
                if (streamResponse != null)
                {
                    streamResponse.Close();
                }
                if (streamRead != null)
                {
                    streamRead.Close();
                }
                if (response != null)
                {
                    response.Close();
                }

                ptr.WebRequest = null;

                if (requestSuccess)
                {
                    MakeNextRequest();
                }
                else
                {
                    if (currentRequest.Callback != null)
                    {
                        currentRequest.Callback(new WebRequestCompletedArgs(status));
                    }
                }
            }
        }