如何通过 httpresponse 读取以获得 return 值:
How do I get read through httpresponse to get return values:
我想我快要让它工作了,但我不确定如何获取内容以找出内容中返回的结果消息是什么,看看它是否是一个好的记录,我只是不知道不知道怎么办。我已经接近...可以在 Visual Studio 当地人中看到我的价值观,但无法接触到他们。
这些是我的数据classes:
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 class:
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 中的任务 class:
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 的地方...好、坏还是更糟?
public 部分 class ApiClient
{
私有只读 HttpClient _httpClient;
私人 Uri BaseEndpoint { 得到;放; }
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
实现。
我想我快要让它工作了,但我不确定如何获取内容以找出内容中返回的结果消息是什么,看看它是否是一个好的记录,我只是不知道不知道怎么办。我已经接近...可以在 Visual Studio 当地人中看到我的价值观,但无法接触到他们。
这些是我的数据classes:
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 class:
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 中的任务 class:
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 的地方...好、坏还是更糟? public 部分 class ApiClient { 私有只读 HttpClient _httpClient; 私人 Uri BaseEndpoint { 得到;放; }
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
实现。