[英]How to call a WebAPI from WinRT
I try to call a WebAPI from WinRT. 我尝试从WinRT调用WebAPI。 In WinRT I wrote serializable classes, which I want to be sent to the WebAPI:
在WinRT中,我编写了可序列化的类,我希望将其发送到WebAPI:
[DataContractAttribute]
public class RecognizeItem
{
[DataMember()]
public string Id { get; set; }
[DataMember()]
public Windows.UI.Xaml.Media.Imaging.BitmapImage Image { get; set; }
}
Next step send data to server: 下一步将数据发送到服务器:
RecognizeItem system = new RecognizeItem()
{
Id = login,
Image = LoadImage
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string json = JsonConvert.SerializeObject(system);
HttpContent content = new StringContent(json);
HttpResponseMessage response = await client.PostAsync("api/RecognizeItem/", content);
In the implementation of the webAPI I have implemented a serializable class as follows: 在webAPI的实现中,我实现了一个可序列化的类,如下所示:
[Serializable]
public class RecognizeItem
{
public string Id { get; set; }
public Bitmap Image { get; set; }
}
And my ApiController: 而我的ApiController:
public string Post([FromBody] RecognizeItem image)...
But in response I see the following status: 但作为回应,我看到以下状态:
StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'
StatusCode:415,ReasonPhrase:'不支持的媒体类型'
How can I fix this error? 我该如何解决这个错误?
I believe Content-Type is a header of the content, not of the request. 我相信Content-Type是内容的标题,而不是请求的标题。
Try setting your content by passing the content-type to the StringContent constructor: 尝试通过将content-type传递给StringContent构造函数来设置内容:
Replace 更换
HttpContent content = new StringContent(json);
by 通过
HttpContent content = new StringContent(json,
Encoding.UTF8,
"application/json");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.