Ejemplo n.º 1
0
        public async virtual Task <T> PostAsync <T, TR>(string Url, TR tParameter, QueryInstance query) where T : class where TR : class
        {
            try
            {
                if (query.CheckParameter())
                {
                    return(null);
                }
                var host = await GetHostsAsync(query);

                var client  = ExtendClass.GetHttpClient(ClientFactory, nameof(NacosHttpClient), host, Nacos.Value.EnableSSL);
                var content = ExtendClass.SetMethodJsonParameter(tParameter);
                content.Headers.Clear();
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json")
                {
                    CharSet = "utf-8"
                };
                var httpResponse = await client.PostAsync(Url, content);

                var resultStr = httpResponse.Content.ReadAsStringAsync().Result;
                var result    = resultStr.ToJson <T>();
                return(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(MethodInfo.GetCurrentMethod().Name + ":" + ex.Message);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public async virtual Task <T> GetAsync <T, TR>(string Url, TR tParameter, QueryInstance query) where T : class where TR : class
        {
            try
            {
                if (query.CheckParameter())
                {
                    return(null);
                }
                var host = await GetHostsAsync(query);

                var client = ExtendClass.GetHttpClient(ClientFactory, nameof(NacosHttpClient), host, Nacos.Value.EnableSSL);
                Url = Url.GetUrl(ExtendClass.SetMethodParameter(tParameter));
                var httpResponse = await client.GetAsync(Url);

                var resultStr = await httpResponse.Content.ReadAsStringAsync();

                var result = resultStr.ToJson <T>();
                return(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(MethodInfo.GetCurrentMethod().Name + ":" + ex.Message);
                return(null);
            }
        }
Ejemplo n.º 3
0
        private async Task <TR> GetAsync <T, TR>(string sUrl, T nacos, string MethodName, Type type) where TR : class
        {
            try
            {
                var client       = HttpClient.CreateClient("NacosConfig");
                var url          = sUrl.GetUrl(ExtendClass.SetMethodParameter(nacos));
                var httpResponse = await client.GetAsync(url);

                var resultStr = await httpResponse.Content.ReadAsStringAsync();

                var result = resultStr.ToJson(type) as TR;
                return(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(MethodName + "执行GetAsync请求出错" + ":" + ex.Message);
                return(null);
            }
        }
Ejemplo n.º 4
0
        private async Task <Result> PutAsync <T>(string sUrl, T nacos, string MethodName) where T : class
        {
            Result result = new Result {
                StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "客户端请求中的语法错误"
            };

            try
            {
                var client  = HttpClient.CreateClient("NacosConfig");
                var message = await client.PutAsync(sUrl, ExtendClass.SetMethodParameter(nacos));

                result.StatusCode = message.StatusCode;
                result.Message    = await message.Content.ReadAsStringAsync();

                return(result);
            }
            catch (HttpRequestException ex)
            {
                result.Message = ex.Message;
                Console.WriteLine(MethodName + "执行PutAsync请求出错" + ":" + ex.Message);
                return(result);
            }
        }