Example #1
0
        public async Task <T> Post <T>(string url, Dictionary <string, object> simpleparams = null, List <Param> complexparams = null)
        {
            try
            {
                Debugger.WriteLine("RestSharp: " + url);
                var client  = new RestSharp.RestClient(url);
                var request = new RestSharp.RestRequest(RestSharp.Method.POST);
                if (simpleparams != null)
                {
                    foreach (var item in simpleparams)
                    {
                        request.AddParameter(item.Key, item.Value);
                    }
                }

                if (complexparams != null)
                {
                    foreach (var item in complexparams)
                    {
                        switch (item.Type)
                        {
                        case ParamType.File:
                            request.AddFileBytes(item.Name, item.Bytes, item.FileName, item.ContentType);
                            break;

                        case ParamType.String:
                            request.AddParameter(item.Name, item.Element);
                            break;
                        }
                    }
                }
                var response = await client.ExecuteTaskAsync(request);

                if (response != null)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Debugger.WriteLine("RestSharp: " + response.Content);
                        return(response.Content.DeserializeObject <T>());
                    }
                }
            }
            catch (Exception ex)
            {
                Debugger.WriteLine("RestSharp: " + ex.StackTrace);
            }
            return(default(T));
        }