Esempio n. 1
0
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Address);

            webRequest.Credentials = Credentials;
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";

            var writer = new StringWriter();

            request.WriteJSON(writer);
            writer.Flush();
            var json  = writer.ToString();
            var bytes = Encoding.UTF8.GetBytes(json);

#if !PORTABLE
            webRequest.ContentLength = bytes.Length;
#endif
            Stream dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Dispose();
            RPCResponse response = null;
            try
            {
                WebResponse webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false);

                response = RPCResponse.Load(webResponse.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    throw;
                }
                response = RPCResponse.Load(ex.Response.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            return(response);
        }
Esempio n. 2
0
        async Task <RPCResponse> SendCommandAsyncCore(RPCRequest request, bool throwIfRPCError)
        {
            RPCResponse response = null;
            var         batches  = _BatchedRequests;

            if (batches != null)
            {
                TaskCompletionSource <RPCResponse> source = new TaskCompletionSource <RPCResponse>();
                batches.Enqueue(Tuple.Create(request, source));
                response = await source.Task.ConfigureAwait(false);
            }
            HttpWebRequest webRequest = response == null?CreateWebRequest() : null;

            if (response == null)
            {
                var writer = new StringWriter();
                request.WriteJSON(writer);
                writer.Flush();
                var json  = writer.ToString();
                var bytes = Encoding.UTF8.GetBytes(json);
#if !(PORTABLE || NETCORE)
                webRequest.ContentLength = bytes.Length;
#endif
                var dataStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

                await dataStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

                await dataStream.FlushAsync().ConfigureAwait(false);

                dataStream.Dispose();
            }
            WebResponse webResponse   = null;
            WebResponse errorResponse = null;
            try
            {
                webResponse = response == null ? await webRequest.GetResponseAsync().ConfigureAwait(false) : null;

                response = response ?? RPCResponse.Load(await ToMemoryStreamAsync(webResponse.GetResponseStream()).ConfigureAwait(false));

                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null || ex.Response.ContentLength == 0)
                {
                    throw;
                }
                errorResponse = ex.Response;
                response      = RPCResponse.Load(await ToMemoryStreamAsync(errorResponse.GetResponseStream()).ConfigureAwait(false));
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            finally
            {
                if (errorResponse != null)
                {
                    errorResponse.Dispose();
                    errorResponse = null;
                }
                if (webResponse != null)
                {
                    webResponse.Dispose();
                    webResponse = null;
                }
            }
            return(response);
        }