Example #1
0
        private static void AsyncReadCallback(IAsyncResult asyncResult)
        {
            RequestState state     = ((RequestState)(asyncResult.AsyncState));
            int          bytesRead = 0;

            try {
                Stream stream = state.response.GetResponseStream();
                if (stream.CanRead)
                {
                    bytesRead = stream.EndRead(asyncResult);
                }

                if (bytesRead > 0)
                {
                    state.BytesRead += bytesRead;
                    state.StreamDst.Write(state.BufferRead, 0, bytesRead);
                    state.StreamDst.Flush();
                    stream.BeginRead(state.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(AsyncReadCallback), state);
                    if (DownloadProgressChanged != null)
                    {
                        DownloadProgressChanged(state, EventArgs.Empty);
                    }
                }
                else
                {
                    state.Close();
                }
            } catch {
                state.Close();
            }
            //allDone.Set();
        }
Example #2
0
        private static void AsyncRespCallback(IAsyncResult asyncResult)
        {
            RequestState requestState = ((RequestState)(asyncResult.AsyncState));

            try {
                HttpWebResponse response = ((HttpWebResponse)(requestState.request.EndGetResponse(asyncResult)));
                if (requestState.response == null)
                {
                    requestState.response = response;
                }
                requestState.TotalBytes = response.ContentLength;
                if (DownloadProgressChanged != null)
                {
                    DownloadProgressChanged(requestState, EventArgs.Empty);
                }
                Stream stream = requestState.response.GetResponseStream();
                stream.BeginRead(requestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(AsyncReadCallback), requestState);
            } catch (WebException e) {
                HMS.LogError("Ошибка асинхронного получения данных с GitHub");
                HMS.LogError(e.Message + " Status: " + e.Status.ToString());
                if (e.Response != null)
                {
                    string resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
                    HMS.LogError(resp);
                }
                requestState.Close();
            } catch (Exception e) {
                HMS.LogError(e.ToString());
                requestState.Close();
            }
            //allDone.Set();
        }
Example #3
0
        private static void StartAsyncRequest(string url, RequestState requestState)
        {
            IAsyncResult result = null;

            try {
                requestState.StreamDst = File.OpenWrite(requestState.File);
                if (!requestState.StreamDst.CanWrite)
                {
                    requestState.Close();
                    return;
                }
                HttpWebRequest request = CreateRequest(url);
                requestState.request = request;
                result = request.BeginGetResponse(new AsyncCallback(AsyncRespCallback), requestState);

                if (result != null)
                {
                    ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true);
                }

                //allDone.WaitOne();
            } catch (WebException e) {
                requestState.Close();
                HMS.LogError("Ошибка асинхронного запроса на GitHub: " + url);
                HMS.LogError(e.Message + " Status: " + e.Status.ToString());
                if (e.Response != null)
                {
                    string resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
                    HMS.LogError(resp);
                }
            } catch (Exception e) {
                requestState.Close();
                HMS.LogError(e.ToString());
            }
        }
Example #4
0
        private static void StartAsyncRequest(string url, RequestState requestState)
        {
            try {
                requestState.StreamDst = File.OpenWrite(requestState.File);
                if (!requestState.StreamDst.CanWrite) {
                    requestState.Close();
                    return;
                }
                HttpWebRequest request = CreateRequest(url);
                requestState.request = request;
                var result = request.BeginGetResponse(AsyncRespCallback, requestState);
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, request, DefaultTimeout, true);
                //allDone.WaitOne();

            } catch (WebException e) {
                requestState.Close();
                HMS.LogError($"Ошибка асинхронного запроса на GitHub: {url}");
                HMS.LogError($"{e.Message} Status: {e.Status}");
                var respStream = e.Response?.GetResponseStream();
                if (respStream != null) {
                    string resp = new StreamReader(respStream).ReadToEnd();
                    HMS.LogError(resp);
                }
            } catch (Exception e) {
                requestState.Close();
                HMS.LogError(e.ToString());

            }
        }
Example #5
0
        private static void StartAsyncRequest(string url, RequestState requestState)
        {
            IAsyncResult result = null;
            try {
                requestState.StreamDst = File.OpenWrite(requestState.File);
                if (!requestState.StreamDst.CanWrite) {
                    requestState.Close();
                    return;
                }
                HttpWebRequest request = CreateRequest(url);
                requestState.request = request;
                result = request.BeginGetResponse(new AsyncCallback(AsyncRespCallback), requestState);

                if (result != null)
                    ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true);

                //allDone.WaitOne();

            } catch (WebException e) {
                requestState.Close();
                HMS.LogError("Ошибка асинхронного запроса на GitHub: " + url);
                HMS.LogError(e.Message + " Status: " + e.Status.ToString());
                if (e.Response != null) {
                    string resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
                    HMS.LogError(resp);
                }

            } catch (Exception e) {
                requestState.Close();
                HMS.LogError(e.ToString());

            }
        }