12 changed files with 213 additions and 29 deletions
@ -0,0 +1,58 @@ |
|||||
|
using System.Net.Http.Headers; |
||||
|
using System.Text; |
||||
|
using EFCoreDatabase.Entities; |
||||
|
|
||||
|
namespace NewWMSProject.Services; |
||||
|
|
||||
|
|
||||
|
|
||||
|
public interface IHttpClientService |
||||
|
{ |
||||
|
public Task<string> PostAsyncJson(string json, string apiAddress); |
||||
|
} |
||||
|
|
||||
|
public class HttpClientService:IHttpClientService |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 使用post方法异步请求WebAPI
|
||||
|
/// </summary>
|
||||
|
/// <param name = "json" > 发送的参数字符串,只能用json</param>
|
||||
|
/// <param name = "apiAddress" ></ param >
|
||||
|
/// < returns > 返回的字符串 </ returns >
|
||||
|
public async Task<string> PostAsyncJson(string json, string apiAddress) |
||||
|
{ |
||||
|
|
||||
|
var responseBody = string.Empty; |
||||
|
bool test0Prd1Flag = true; |
||||
|
|
||||
|
//保证接口处于测试服务状态下
|
||||
|
//WebAPI接口地址
|
||||
|
string url = apiAddress; |
||||
|
using (HttpClient client = new HttpClient()) |
||||
|
{ |
||||
|
HttpContent content = new StringContent(json); |
||||
|
//设置传输数据类型为json
|
||||
|
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
||||
|
client.Timeout = new TimeSpan(1, 0, 0, 0, 0); |
||||
|
client.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); |
||||
|
client.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600"); |
||||
|
client.DefaultRequestHeaders.Add("ContentType", "application/json"); |
||||
|
client.DefaultRequestHeaders.Add("Accept", "*/*"); |
||||
|
|
||||
|
//设置用户名密码
|
||||
|
//AuthenticationHeaderValue authentication = new AuthenticationHeaderValue("Basic",
|
||||
|
// Convert.ToBase64String(Encoding.UTF8.GetBytes(test0Prd1Flag ? $"{HANDApiUsernameTest}:{HANDApiPasswordTest}" : $"{HANDApiUsername}:{HANDApiPassword}")));
|
||||
|
//client.DefaultRequestHeaders.Authorization = authentication;
|
||||
|
|
||||
|
HttpResponseMessage response = await client.PostAsync(url, content); |
||||
|
|
||||
|
if (response.IsSuccessStatusCode) |
||||
|
{ |
||||
|
response.EnsureSuccessStatusCode(); |
||||
|
responseBody = await response.Content.ReadAsStringAsync(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
return responseBody; |
||||
|
} |
||||
|
} |
@ -1,9 +1,32 @@ |
|||||
{ |
{ |
||||
"Logging": { |
|
||||
"LogLevel": { |
|
||||
|
"Serilog": { |
||||
|
"MinimumLevel": { |
||||
"Default": "Information", |
"Default": "Information", |
||||
"Microsoft.AspNetCore": "Warning" |
|
||||
} |
|
||||
|
"Override": { |
||||
|
"Microsoft": "Warning", |
||||
|
"Microsoft.Hosting.Lifetime": "Information" |
||||
|
} |
||||
|
}, |
||||
|
"Enrich": [ "FromLogContext" ], |
||||
|
"WriteTo": [ |
||||
|
{ |
||||
|
"Name": "File", |
||||
|
"Args": { |
||||
|
"path": "./Logs/Log-.txt", |
||||
|
"rollingInterval": "Day" |
||||
|
} |
||||
|
|
||||
|
}, |
||||
|
{ |
||||
|
"Name": "Console", |
||||
|
"Args": {} |
||||
|
}, |
||||
|
{ |
||||
|
"Name": "Debug", |
||||
|
"Args": {} |
||||
|
} |
||||
|
] |
||||
}, |
}, |
||||
"AllowedHosts": "*" |
|
||||
|
"AllowedHosts": "*", |
||||
|
"Urls": "http://localhost:9003" |
||||
} |
} |
||||
|
Loading…
Reference in new issue