lvzb
5 months ago
10 changed files with 254 additions and 94 deletions
@ -0,0 +1,8 @@ |
|||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|||
{ |
|||
public class AuthenticationOptions |
|||
{ |
|||
public string Username { get; set; } |
|||
public string Password { get; set; } |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|||
{ |
|||
public class BoomiResult |
|||
{ |
|||
public string TraceId { get; set; } |
|||
// public int ErrorCode { get; set; }
|
|||
public string ErrorMessage { get; set; } |
|||
// public string Information { get; set; }
|
|||
public string Status { get; set; } |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|||
{ |
|||
public interface IPostService:ITransientDependency |
|||
{ |
|||
Task<BoomiResult> PostAsync(string baseUrl, string apiUrl, string content, string username, string password); |
|||
} |
|||
} |
@ -0,0 +1,78 @@ |
|||
using System; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Headers; |
|||
using System.Net.Http.Json; |
|||
using System.Text; |
|||
using System.Text.Json; |
|||
using System.Threading.Tasks; |
|||
using Serilog; |
|||
|
|||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|||
{ |
|||
public class PostService : IPostService |
|||
{ |
|||
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory; |
|||
|
|||
public PostService(System.Net.Http.IHttpClientFactory httpClientFactory) |
|||
{ |
|||
_httpClientFactory = httpClientFactory; |
|||
} |
|||
|
|||
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); |
|||
return result; |
|||
} |
|||
|
|||
public async Task<BoomiResult> PostAsync(string baseUrl,string apiUrl, string content, AuthenticationOptions options) |
|||
{ |
|||
//var client = new HttpClient();
|
|||
var client = _httpClientFactory.CreateClient(); |
|||
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;
|
|||
var url = baseUrl; |
|||
Serilog.Log.Information(url); |
|||
Serilog.Log.Debug(content); |
|||
var response = await client.PostAsync(url, httpContent); |
|||
|
|||
try |
|||
{ |
|||
Log.Debug("before EnsureSuccessStatusCode() response content↓↓↓"); |
|||
Log.Debug(await response.Content.ReadAsStringAsync()); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
Console.WriteLine(e); |
|||
throw; |
|||
} |
|||
|
|||
response.EnsureSuccessStatusCode(); |
|||
|
|||
HttpContent respCon = response.Content; |
|||
|
|||
string resultContent = await respCon.ReadAsStringAsync(); |
|||
resultContent = resultContent.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""); |
|||
|
|||
Serilog.Log.Debug("response content↓↓↓"); |
|||
Serilog.Log.Debug(resultContent); |
|||
|
|||
//var result = await respCon.ReadFromJsonAsync<BoomiResult>();
|
|||
var result = JsonSerializer.Deserialize<BoomiResult>(resultContent); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue