Exemple #1
0
        private async Task <string> CallServer(string api, Dictionary <string, string> paramters, Dictionary <string, string> body, string curServer, HttpMethod method)
        {
            InjectSecurityInfo(paramters);

            var headers = NamingHttpUtil.BuildHeader();

            var url = string.Empty;

            if (curServer.StartsWith(UtilAndComs.HTTPS) || curServer.StartsWith(UtilAndComs.HTTP))
            {
                url = curServer.TrimEnd('/') + api;
            }
            else
            {
                if (IPUtil.ContainsPort(curServer))
                {
                    curServer = curServer + IPUtil.IP_PORT_SPLITER + serverPort;
                }

                // TODO http or https
                url = UtilAndComs.HTTP + curServer + api;
            }

            try
            {
                var client = _clientFactory?.CreateClient(ConstValue.ClientName) ?? new HttpClient();
                client.Timeout = TimeSpan.FromSeconds(8);
                var requestUrl     = $"{url}?{InitParams(paramters, body)}";
                var requestMessage = new HttpRequestMessage(method, requestUrl);

                BuildHeader(requestMessage, headers);

                var responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false);

                if (responseMessage.IsSuccessStatusCode)
                {
                    var content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(content);
                }
                else if (responseMessage.StatusCode == System.Net.HttpStatusCode.NotModified)
                {
                    return(string.Empty);
                }

                // response body will contains some error message
                var msg = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                throw new NacosException((int)responseMessage.StatusCode, $"{responseMessage.StatusCode}--{msg}");
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "[NA] failed to request");
                throw new NacosException(NacosException.SERVER_ERROR, ex.Message);
            }
        }