Exemplo n.º 1
0
        public static Task <HttpClientConnection> NewConnection(HttpClientConnectionOptions options)
        {
            options.Validate();

            var bootstrap = new ConnectionBootstrap();

            options.ConnectionSetup += (sender, e) => {
                if (e.ErrorCode != 0)
                {
                    var message = CRT.ErrorString(e.ErrorCode);
                    bootstrap.TaskSource.SetException(new WebException(String.Format("Failed to connect: {0}", message)));
                }
                else
                {
                    bootstrap.TaskSource.SetResult(bootstrap.Connection);
                }
            };

            bootstrap.Connection = new HttpClientConnection(options);
            return(bootstrap.TaskSource.Task);
        }
Exemplo n.º 2
0
        public Task <StreamResult> MakeRequest(HttpRequest request, HttpResponseStreamHandler responseHandler)
        {
            var bootstrap = new StreamBootstrap();

            responseHandler.StreamComplete += (sender, e) => {
                streams.Remove(bootstrap.Stream);
                if (e.ErrorCode != 0)
                {
                    var message = CRT.ErrorString(e.ErrorCode);
                    bootstrap.TaskSource.SetException(new WebException($"Stream {bootstrap.Stream} failed: {message}"));
                }
                else
                {
                    bootstrap.TaskSource.SetResult(new StreamResult(e.ErrorCode));
                }
            };

            bootstrap.Stream = new HttpClientStream(this, request, responseHandler);
            streams.Add(bootstrap.Stream);
            bootstrap.Stream.Activate();

            return(bootstrap.TaskSource.Task);
        }