You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.7 KiB
46 lines
1.7 KiB
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<BoomiResult> 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<BoomiResult> 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<BoomiResult>().ConfigureAwait(false);
|
|
return result;
|
|
}
|
|
}
|
|
|