using System; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using System.Threading.Tasks; namespace Win_in.Sfs.Wms.DataExchange.Iac.QadAgent.PostServices; public class PostService : IPostService { public async Task PostAsync(string baseUrl, string apiUrl, string content, string username, string password) { var options = new AuthenticationOptions() { Username = username, Password = password }; var result = await PostAsync(baseUrl, apiUrl, content, options).ConfigureAwait(false); return result; } public async Task PostAsync(string baseUrl, string apiUrl, string content, AuthenticationOptions options) { var client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var byteArray = Encoding.ASCII.GetBytes($"{options.Username}:{options.Password}"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var httpContent = new StringContent(content); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var url = baseUrl + "/" + apiUrl; Serilog.Log.Information(url); Serilog.Log.Debug(content); var response = await client.PostAsync(url, httpContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadFromJsonAsync().ConfigureAwait(false); return result; } }