コード例 #1
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
     }
 }
コード例 #2
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(Credentials.UserName + ":" + Credentials.Password));
            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);

            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);
        }