Beispiel #1
0
        /// <summary>
        /// Event handler for the web client's requestion completed event. Reads
        /// the request's result information and subsequently triggers
        /// the UpdateDataAvailable event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadResult(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                if (null == e || e.Error != null)
                {
                    Error = "Unspecified error";
                    if (null != e && (null != e.Error))
                    {
                        Error = e.Error.Message;
                    }
                }

                using (var sr = new StreamReader(e.Result))
                {
                    Data = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Error = string.Empty;
                Data  = string.Empty;

                manager.OnLog(new LogEventArgs("The update request could not be completed.", LogLevel.File));
                manager.OnLog(new LogEventArgs(ex, LogLevel.File));
            }

            //regardless of the success of the above logic
            //invoke the completion callback
            OnRequestCompleted.Invoke(this);
        }
        public override void HandleReceive(byte[] receiveBuffer, int offset, int count)
        {
            AppendReceivedBytes(receiveBuffer, offset, count);

            for (int testIndex = 0; testIndex < _RequestCompleteTests.Count; testIndex++)
            {
                byte[] requestCompleteTest = _RequestCompleteTests[testIndex];
                if (_Buffer.Count < requestCompleteTest.Length)
                {
                    continue;
                }

                bool isCompleted = true;
                for (int i = 0; i < requestCompleteTest.Length; i++)
                {
                    if (_Buffer[_Buffer.Count - 1 - i] != requestCompleteTest[requestCompleteTest.Length - 1 - i])
                    {
                        isCompleted = false;
                        break;
                    }
                }

                if (isCompleted && OnRequestCompleted != null)
                {
                    try
                    {
                        OnRequestCompleted.Invoke(_Client, testIndex, _Buffer.ToArray());
                    }
                    catch
                    {
                    }
                    _Buffer.Clear();
                }
            }
        }
        private void PerformRequest(CustomStream data, int index)
        {
            using (MemoryStream s1 = data.GetStream())
            {
                OnRequestCreated?.Invoke($"starting request {index} with datalength: {data.Length}");
                RestRequest request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.Files.Add(new FileParameter
                {
                    Name          = "bestandje",
                    Writer        = (s) => { s1.CopyTo(s); },
                    FileName      = "kankerscammers.jpg",
                    ContentLength = data.Length
                });

                DateTime      begin    = DateTime.Now;
                IRestResponse response = Client.Execute(request);
                DateTime      end      = DateTime.Now;
                TimeSpan      span     = end - begin;
                string        time     = $" with time {span.TotalMilliseconds}ms";
                if (response == null)
                {
                    OnRequestCompleted?.Invoke("Request " + index + " completed with no result" + time);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    OnRequestCompleted?.Invoke("Request " + index + " completed OK" + time);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
                {
                    OnRequestCompleted?.Invoke("Request " + index + " completed with internal server error" + time);
                }
                else
                {
                    OnRequestCompleted?.Invoke("Request " + index + " completed with other result: " + response.StatusCode + time);
                }
                response = null;
                request  = null;
                data     = null;
                s1.Dispose();
                GC.Collect();
            }
        }