我认为我即将实现这一点,但不确定如何获取内容,以了解内容中返回的结果消息,看看它是否是一个好记录,我只是不知道如何。我已经接近了。。。可以在Visual Studio本地设置中查看我的值,但无法访问它们。
这些是我的数据类:
public class PurchaseOrder
{
private List<POHeader> _headers = new List<POHeader>();
private List<PODetail> _details = new List<PODetail>();
[JsonProperty("HeaderAttributes")]
public List<POHeader> Header
{
get { return _headers; }
set { _headers = value; }
}
[JsonProperty("LineAttributes")]
public List<PODetail> Details
{
get { return _details; }
set { _details = value; }
}
}
public class POHeader
{
private string _poNumber;
private int _resultCode;
private string _resultMessage;
[JsonProperty("po_number")]
public string PONumber
{
get { return _poNumber; }
set { _poNumber = value; }
}
[JsonProperty("result_code")]
public int ResultCode
{
get { return _resultCode; }
set { _resultCode = value; }
}
[JsonProperty("result_msg")]
public string ResultMsg
{
get { return _resultMessage; }
set { _resultMessage = value; }
}
}
这在APIClient类中:
public async Task<HttpResponseMessage> PostStreamAsync(string requestUrl, CancellationToken cancellationToken, object content)
{
addHeaders();
using (var request = new HttpRequestMessage(HttpMethod.Post, requestUrl))
using (var httpContent = CreateHttpContentForStream(content))
{
request.Content = httpContent;
using (var response = await _httpClient
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
return response;
}
}
}
这是我在TransmitPO类中的任务:
public static async Task TransmitToService(List<PurchaseOrder> orders, CancellationToken cancellationToken, string path)
{
ApiClient _client = new ApiClient(new Uri("myUrl"), "code");
var results = await _client.PostStreamAsync(path, cancellationToken, orders);
var deserializedResults = JsonConvert.DeserializeObject<List<PurchaseOrder>>(results);
}
当它通过过程代码运行时,我就是这样称呼它的:
await TransmitToService(poList, CancellationToken.None, poPath);
这是我创建HttpClient的地方。。。好,坏,更糟?公共分部类ApiClient{private只读HttpClient _HttpClient;private Uri BaseEndpoint{get;set;}
private string AuthToken { get; set; }
public ApiClient(Uri baseEndpoint, string bToken)
{
BaseEndpoint = baseEndpoint;
AuthToken = bToken;
_httpClient = new HttpClient();
}
}
您试图反序列化(已处理的(HttpResponseMessage
,而不是其内容。
private static readonly HttpClient _httpClient = new HttpClient();
public async Task<T> PostStreamAsync<T>(string requestUrl, CancellationToken cancellationToken, object content)
{
addHeaders();
using (var request = new HttpRequestMessage(HttpMethod.Post, requestUrl))
using (var httpContent = CreateHttpContentForStream(content))
{
request.Content = httpContent;
using (var response = await _httpClient
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
}
}
var deserializedResults = _client.PostStreamAsync<List<PurchaseOrder>>(path, cancellationToken, orders);
此外,您应该确保不会为每个请求创建新的HttpClient
,否则您将获得不稳定的ApiClient
实现。