我遇到了使用我的代码从API获取响应的问题,请求没有超时,它根本没有给我回复。我在我自己的API中创建了一个api端点,以返回json字符串,然后使用" Firefox Poster"手动发布json数据。它工作得很好。有了这个,我相信问题就在我的代码中。
我得到了一个C#WebAPI,我正在开发它与Angular前端一起使用(这是有效的,它只适用于历史)。在调用我的API时,我创建了对象" EnrollmentRequestDto"
public class EnrollmentRequestDto
{
/// <summary>
/// Context Information for the request. Contains the Ship-To, language code and timezone.
/// </summary>
[JsonProperty("requestContext")]
public RequestContextDto RequestContext { get; set; }
/// <summary>
/// Unique ID provided by DEP to reseller on provisioning DEP access. This would be the reseller's DEP ID if its posted by distributor OBO a reseller, and would be his own depResellerId if a reseller is posting for self.
/// </summary>
[JsonProperty("depResellerId")]
public string DepResellerId { get; set; }
/// <summary>
/// Unique transaction ID provided by the reseller
/// </summary>
[JsonProperty("transactionId")]
public string TransactionId { get; set; }
/// <summary>
/// List of orders in the transaction (limit 1000 per transaction)
/// </summary>
[JsonProperty("orders")]
public List<OrderDto> Orders { get; set; }
}
创建此对象后,我发送到我的类RequestHandler和方法BulkEnrollRequest,其中atm是用HttpClient扩展名Flurl编写的,可以在这里找到:github
public IResponse BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
result.Wait();
return result.Result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
我也试过这个以确保在Flurl中没有任何反应(这只是为了调试到我想要得到响应的地方):
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
var json = JsonConvert.SerializeObject(enrollmentRequest);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.PostAsync(_bulkEnrollUrl, new StringContent(json, Encoding.UTF8, "application/json"));
return new SuccessResponse();
}
代码在等待点冻结,没有任何事情发生。我试图在BulkEnrollRequest之后放置一个断点,所以它永远不会离开这个方法。
现在这里有趣的部分:它在我使用ErrorHandler时工作,并且在某些时候API刚刚停止响应。 Wireshark显示正在提出请求......所以我被困住了。
感谢任何帮助。
修改
这现在有效!我从API控制器到RequestHandler一直实现了 async ,然后等待每次调用。供参考:
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
return result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
答案 0 :(得分:5)
现在已修复。我相信Wireshark流量看起来如此奇怪的原因是由于代码中的死锁,并且超时在我身边,这意味着我永远无法确认信息。为了解决这个问题,我在所有方法上实现了 async ,从Controller到我的RequestHandler类。供参考:
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
return result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
答案 1 :(得分:0)
试试此代码
安装包Microsoft.AspNet.WebApi.Client
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync("api/products", enrollmentRequest);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<T>();
}
}