我有一个可以使用此cURL
命令调用的api:
C:\Windows\System32\curl.exe -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Bearer X.Y.Z -d '{
>> "Filter": {
>> "ShowRent": true,
>> "ShowSale": true,
>> "ShowApartment": true,
>> "ShowOffice": true,
>> },
>> "PageSize": 1,
>> "PageNumber": 1
>> }' http://localhost:port_number/path_to_api
此请求已成功完成。
该api应该返回一个List<ResultDto>
。
我想在Xamarin项目中创建相同的请求。
这是我尝试过的:
var a = new
{
PageSize = 5,
PageNumber = 1,
Filter = new
{
ShowRent = true,
ShowSale = true,
ShowApartment = true,
ShowOffice = true
}
};
var json = JsonConvert.SerializeObject(a);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer",
my_access_token);
var rawResponse = await client.PostAsync(
"http://localhost:port_number/path_to_api",
stringContent
);
List<RealtorEstateMarkerDto> result=
JsonConvert.DeserializeObject<List<ResultDto>>(
await rawResponse.Content.ReadAsStringAsync());
运行我的xamarin代码时,服务器上发生System.OperationCanceledException
异常。
这是异常的堆栈跟踪:
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Owin.IOwinContextExtensions.GetDependencyResolver(IOwinContext context)