Beispiel #1
0
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            try
            {
                return(await SendCommandAsyncCore(request, throwIfRPCError).ConfigureAwait(false));
            }
#if !NOFILEIO
            catch (WebException ex)
            {
                if (!ex.Message.Contains("401"))
                {
                    throw;
                }
                if (_FromCookiePath != null)
                {
                    try
                    {
                        _Authentication = File.ReadAllText(_FromCookiePath);
                    }
                    catch { throw ex; }
                }
                return(await SendCommandAsyncCore(request, throwIfRPCError).ConfigureAwait(false));
            }
#else
            catch (WebException)
            {
                throw;
            }
#endif
        }
Beispiel #2
0
 public RPCResponse SendCommand(RPCRequest request, bool throwIfRPCError = true)
 {
     try
     {
         return(SendCommandAsync(request, throwIfRPCError).Result);
     }
     catch (AggregateException aex)
     {
         ExceptionDispatchInfo.Capture(aex.InnerException).Throw();
         return(null);                //Can't happen
     }
 }
Beispiel #3
0
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(Address);

            webRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + Encoders.Base64.EncodeData(Encoders.ASCII.DecodeData(_Authentication));
            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 || 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();
            RPCResponse response;
            try
            {
                using (var webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false))
                {
                    response = RPCResponse.Load(webResponse.GetResponseStream());
                }
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null || ex.Response.ContentLength == 0)
                {
                    throw;
                }
                response = RPCResponse.Load(ex.Response.GetResponseStream());
                if (throwIfRPCError)
                {
                    response.ThrowIfError();
                }
            }
            return(response);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }