示例#1
0
        public async Task RunAsync(CancellationToken token = default(CancellationToken))
        {
            var request = CreateRequest();

            if (OnWriteAsync != null)
            {
                await OnWriteAsync.Invoke(request, token);
            }
            else
            {
                OnWrite?.Invoke(request);
            }

            using (token.Register(() => request.Abort(), false))
                using (var response = (HttpWebResponse)await request.GetResponseAsync()) {
                    token.ThrowIfCancellationRequested();

                    if (OnReadAsync != null)
                    {
                        await OnReadAsync.Invoke(response, token);
                    }
                    else
                    {
                        OnRead?.Invoke(response);
                    }
                }
        }
示例#2
0
        public void Run()
        {
            var request = CreateRequest();

            if (OnWrite != null)
            {
                OnWrite.Invoke(request);
            }
            else
            {
                OnWriteAsync?.Invoke(request, CancellationToken.None).GetAwaiter().GetResult();
            }

            using (var response = (HttpWebResponse)request.GetResponse()) {
                if (OnRead != null)
                {
                    OnRead?.Invoke(response);
                }
                else
                {
                    OnReadAsync?.Invoke(response, CancellationToken.None).GetAwaiter().GetResult();
                }
            }
        }
示例#3
0
        public async Task RunAsync()
        {
            var request = CreateRequest();

            if (OnWriteAsync != null)
            {
                await OnWriteAsync.Invoke(request);
            }
            else if (OnWrite != null)
            {
                OnWrite.Invoke(request);
            }

            using (var response = (HttpWebResponse)await request.GetResponseAsync()) {
                if (OnReadAsync != null)
                {
                    await OnReadAsync.Invoke(response);
                }
                else if (OnRead != null)
                {
                    OnRead?.Invoke(response);
                }
            }
        }