示例#1
0
        public async Task <RPCResponse> SendCommandAsync(RPCRequest request, bool throwIfRPCError = true)
        {
            RPCResponse response;
            var         url = string.Format("{0}{1}", Address, request.Method);

            NameValueCollection outgoingQueryString             = System.Web.HttpUtility.ParseQueryString(String.Empty);
            IList <KeyValuePair <string, string> > keyValuepair = new List <KeyValuePair <string, string> >();

            for (int i = 0; i < request.ParamNames.Count(); i++)
            {
                outgoingQueryString.Add(request.ParamNames[i], request.Params[i].ToString());

                keyValuepair.Add(new KeyValuePair <string, string>(request.ParamNames[i], request.Params[i].ToString()));
            }

            try
            {
                HttpClient client = new HttpClient();
                var        result = await client.PostAsync(url, new FormUrlEncodedContent(keyValuepair));

                if (!result.IsSuccessStatusCode)
                {
                    RPCErrorCode errorCode;
                    switch (result.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                        errorCode = RPCErrorCode.RPC_INVALID_REQUEST;
                        break;

                    case HttpStatusCode.Forbidden:
                        errorCode = RPCErrorCode.RPC_FORBIDDEN_BY_SAFE_MODE;
                        break;

                    case HttpStatusCode.InternalServerError:
                        errorCode = RPCErrorCode.RPC_INTERNAL_ERROR;
                        break;

                    default:
                        errorCode = RPCErrorCode.RPC_MISC_ERROR;
                        break;
                    }

                    response = new RPCResponse(null, new RPCError {
                        Code = errorCode, Message = result.ReasonPhrase
                    });
                }
                else
                {
                    response = new RPCResponse(await result.Content.ReadAsStringAsync(), null);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }

            return(response);
        }
示例#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
     }
 }