【发布时间】:2014-09-11 16:07:42
【问题描述】:
我想将一个 jsonobject 发布到我的 webapi。使用普通的 C# 代码,我会在我的 httpClient 对象上使用 PostAsJsonAsync。但这似乎不受 xamarin 支持。所以我的问题是如何使用 xamarin webclient 或 webrequest 将 json 发布到我的 api?
这是我现在返回 null 的代码...
protected async override void OnResume()
{
base.OnResume();
var clientRequest = new ResourceByNameRequest
{
Name = "G60",
UserId = "1"
};
HttpContent myContent = new StringContent(clientRequest.ToString());
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("XXX");
var cancellationToken = new CancellationToken();
var response = client.PostAsync("/api/Resource/ResourceByName", myContent, cancellationToken).Result;
var result = await response.Content.ReadAsStreamAsync();
//return result;
}
}
catch (Exception)
{
throw;
}
}
【问题讨论】:
标签: c# android json asp.net-web-api xamarin