var result=PostUetds<InputModel, OutputModel>(Url, model, "UserName", "Password", "headers").Result;
public static async Task<TOut> PostUetds<TIn, TOut>(string uri, TIn content, string userName, string password,string headers)
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (headers != null)
{
client.DefaultRequestHeaders.Add("Operation", headers);
}
var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var serialized = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
using HttpResponseMessage response = await client.PostAsync(uri, serialized);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TOut>(responseBody);
}
2. yol
class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync());
Console.ReadLine();
}
static async Task MainAsync()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("URL Adresi");
client.DefaultRequestHeaders.Add("Authorization", "TOKEN");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Operation", "YeniSeferEkleV3"); // Ek olarak header da paramatre varsa bu şekilde tanımlanır
string json = "";
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
var result = await client.PostAsync("", httpContent);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
}
}
catch (Exception ex)
{
throw;
}
}
}