|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
using Org.BouncyCastle.Ocsp; |
|
|
|
|
using System.Net.Http.Headers; |
|
|
|
|
using System.Net.Http.Json; |
|
|
|
|
using System.Text.Json; |
|
|
|
|
@ -89,14 +90,29 @@ namespace ThirdPartyServices.DomainService.ShenZhouShengAn |
|
|
|
|
{ |
|
|
|
|
HttpClientResult<BranchResDto> result = new HttpClientResult<BranchResDto>() { Code = "Success", Message = "请求成功" }; |
|
|
|
|
|
|
|
|
|
result = await SendAndParseAsync<int, HttpClientResult<BranchResDto>>( |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 构造表单参数(key必须是"ubpid",和Postman一致) |
|
|
|
|
var formData = new Dictionary<string, string> |
|
|
|
|
{ |
|
|
|
|
{ "ubpid", ubpid.ToString() } // 转换为字符串,匹配表单提交格式 |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// 调用表单提交方法,而非JSON提交 |
|
|
|
|
result = await SendFormAndParseAsync<HttpClientResult<BranchResDto>>( |
|
|
|
|
"https://zrh.szdunan.cn/v1/api/branch/index_no_check_branch", |
|
|
|
|
token, |
|
|
|
|
ubpid, |
|
|
|
|
formData, |
|
|
|
|
HttpMethod.Post); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
_logger.LogError(ex, "获取分支权限失败"); |
|
|
|
|
return new HttpClientResult<BranchResDto> { Code = "Error", Message = $"请求异常:{ex.Message}" }; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 请求第三方API,发送JSON数据,不支持get和delete方法 |
|
|
|
|
@ -110,10 +126,7 @@ namespace ThirdPartyServices.DomainService.ShenZhouShengAn |
|
|
|
|
public async Task<string> SendJsonAsync<TRequest>(string url, string? token, TRequest? payload, HttpMethod method) |
|
|
|
|
{ |
|
|
|
|
using var request = new HttpRequestMessage(method, url); |
|
|
|
|
if (!string.IsNullOrWhiteSpace(token)) |
|
|
|
|
{ |
|
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
} |
|
|
|
|
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token); |
|
|
|
|
// 写入 JSON 请求体(POST、PUT、PATCH 等适用) |
|
|
|
|
if (payload != null && method != HttpMethod.Get) |
|
|
|
|
{ |
|
|
|
|
@ -166,5 +179,50 @@ namespace ThirdPartyServices.DomainService.ShenZhouShengAn |
|
|
|
|
{ |
|
|
|
|
return SendAndParseAsync<object, TResponse>(url, token, null, method); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 发送表单格式请求(适配 multipart/form-data) |
|
|
|
|
/// </summary> |
|
|
|
|
public async Task<string> SendFormDataAsync(string url, string? token, Dictionary<string, string> formData, HttpMethod method) |
|
|
|
|
{ |
|
|
|
|
using var request = new HttpRequestMessage(method, url); |
|
|
|
|
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token); |
|
|
|
|
request.Headers.TryAddWithoutValidation("Accept", "*/*"); |
|
|
|
|
|
|
|
|
|
// 构造 multipart/form-data 表单数据 |
|
|
|
|
var formContent = new MultipartFormDataContent(); |
|
|
|
|
foreach (var item in formData) |
|
|
|
|
{ |
|
|
|
|
// 添加表单参数(key=ubpid, value=31) |
|
|
|
|
formContent.Add(new StringContent(item.Value), item.Key); |
|
|
|
|
} |
|
|
|
|
request.Content = formContent; |
|
|
|
|
|
|
|
|
|
var response = await _httpClient.SendAsync(request); |
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
|
{ |
|
|
|
|
var error = await response.Content.ReadAsStringAsync(); |
|
|
|
|
throw new Exception($"请求失败:{response.StatusCode}, 内容:{error}"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return await response.Content.ReadAsStringAsync(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 表单请求 + 解析结果(适配 BranchResDto) |
|
|
|
|
/// </summary> |
|
|
|
|
public async Task<TResponse> SendFormAndParseAsync<TResponse>( |
|
|
|
|
string url, |
|
|
|
|
string? token, |
|
|
|
|
Dictionary<string, string> formData, |
|
|
|
|
HttpMethod method) |
|
|
|
|
{ |
|
|
|
|
var responseContent = await SendFormDataAsync(url, token, formData, method); |
|
|
|
|
return JsonSerializer.Deserialize<TResponse>(responseContent, new JsonSerializerOptions |
|
|
|
|
{ |
|
|
|
|
PropertyNameCaseInsensitive = true // 忽略大小写匹配(避免字段名大小写问题) |
|
|
|
|
}) ?? throw new Exception("反序列化返回结果失败"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |