private async Task <HttpResponseMessage> CallServerAsync(HttpMethod httpMethod, string server, string path, Dictionary <string, string> headers, Dictionary <string, string> paramValues, int timeout) { var requestMessage = new HttpRequestMessage { Method = httpMethod }; InjectSecurityInfo(requestMessage, paramValues); requestMessage.Headers.TryAddWithoutValidation("Client-Version", ConstValue.ClientVersion); requestMessage.Headers.TryAddWithoutValidation("User-Agent", ConstValue.ClientVersion); requestMessage.Headers.TryAddWithoutValidation("RequestId", Guid.NewGuid().ToString()); requestMessage.Headers.TryAddWithoutValidation("Request-Module", ConstValue.RequestModule); var client = _clientFactory.CreateClient(ConstValue.ClientName); client.Timeout = TimeSpan.FromMilliseconds(timeout); var requestUrl = string.Empty; if (server.StartsWith(ConstValue.HTTPS, StringComparison.OrdinalIgnoreCase) || server.StartsWith(ConstValue.HTTP, StringComparison.OrdinalIgnoreCase)) { requestUrl = $"{server}{path}"; } else { if (server.Contains(ConstValue.SERVER_ADDR_IP_SPLITER)) { server = server + ConstValue.SERVER_ADDR_IP_SPLITER + _serverPort; } requestUrl = $"http://{server}{path}"; } if (paramValues != null && paramValues.Any()) { if (httpMethod == HttpMethod.Post) { requestMessage.RequestUri = new Uri(requestUrl); requestMessage.Content = new FormUrlEncodedContent(paramValues); } else { var query = HttpAgentCommon.BuildQueryString(paramValues); requestMessage.RequestUri = new Uri($"{requestUrl}?{query}"); } } var responseMessage = await client.SendAsync(requestMessage); if (responseMessage.IsSuccessStatusCode || responseMessage.StatusCode == System.Net.HttpStatusCode.NotModified) { return(responseMessage); } throw new Nacos.Exceptions.NacosException((int)responseMessage.StatusCode, ""); }
public override async Task <HttpResponseMessage> ReqApiAsync(HttpMethod httpMethod, string path, Dictionary <string, string> headers, Dictionary <string, string> paramValues, int timeout) { using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(timeout); var requestMessage = new HttpRequestMessage { Method = httpMethod }; var currentServerAddr = _serverListMgr.GetCurrentServerAddr(); var requestUrl = GetUrl(currentServerAddr, path); InjectSecurityInfo(requestMessage, paramValues); if (paramValues != null && paramValues.Any()) { if (httpMethod == HttpMethod.Post) { requestMessage.RequestUri = new Uri(requestUrl); requestMessage.Content = new FormUrlEncodedContent(paramValues); } else { var query = HttpAgentCommon.BuildQueryString(paramValues); requestMessage.RequestUri = new Uri($"{requestUrl}?{query}"); } } HttpAgentCommon.BuildHeader(requestMessage, headers); HttpAgentCommon.BuildSpasHeaders(requestMessage, paramValues, _options.AccessKey, _options.SecretKey); var responseMessage = await client.SendAsync(requestMessage); if (responseMessage.StatusCode == System.Net.HttpStatusCode.InternalServerError || responseMessage.StatusCode == System.Net.HttpStatusCode.BadGateway || responseMessage.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) { System.Diagnostics.Trace.TraceError("[NACOS ConnectException] currentServerAddr: {0}, httpCode: {1}", _serverListMgr.GetCurrentServerAddr(), responseMessage.StatusCode); } else { _serverListMgr.UpdateCurrentServerAddr(currentServerAddr); return(responseMessage); } throw new System.Net.Http.HttpRequestException($"no available server, currentServerAddr : {currentServerAddr}"); } }