【发布时间】:2015-09-09 06:54:04
【问题描述】:
使用 Xamarin Studio,使用 Azure 移动服务 + .NET 后端自定义托管 API (WebApi 2) 制作 Xamarin.Android 应用。我知道我的移动服务正在运行,因为我可以在日志中看到我的应用正在点击它。这是我的自定义 api 的签名:
[HttpPost]
[Route("postplant")]
public HttpResponseMessage PostPlant([FromBody] string imageInBase64)
我正在尝试使用 InvokeApiAsync 触发此操作,尝试了一些重载,但我尝试使用 Raw Http one。我的字符串是一个已转换为 base 64 的 jpg;如果我将字符串直接输入到我的移动服务测试站点,它可以正常工作。
我的问题是,我收到 415 不支持的实体媒体类型(文本/纯文本)错误。
Message='UserMessage='请求实体的媒体类型'text/plain'是 此资源不支持。'',Status=415 (UnsupportedMediaType), Exception=System.Web.Http.HttpResponseException: 的处理 HTTP 请求导致异常。
这是我在 Xamarin 中的调用:
HttpContent content = new StringContent(imageInBase64, System.Text.Encoding.UTF8);
HttpResponseMessage resp = await _service.InvokeApiAsync(@"jv/postplant", content, HttpMethod.Post, null, null);
我还尝试了以下版本(以及以下版本的组合)将内容类型明确定义为 application/json;这些命中 API OK,但输入参数为 null,因此该方法失败:
HttpContent content = new StringContent(imageInBase64, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage resp = await _service.InvokeApiAsync(@"jv/postplant", content, HttpMethod.Post, null, null);
和
HttpContent content = new StringContent(imageInBase64, System.Text.Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage resp = await _service.InvokeApiAsync(@"jv/postplant", content, HttpMethod.Post, null, null);
和
HttpContent content = new StringContent(imageInBase64, System.Text.Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
Dictionary<string, string> reqHeaders = new Dictionary<string, string>();
reqHeaders.Add("ContentType", "application/json; charset=UTF-8");
HttpResponseMessage resp = await _service.InvokeApiAsync(@"jv/postplant", content, HttpMethod.Post, reqHeaders, null);
我也尝试过将字符串转换为 json 格式(如 {"imageInBase64"="xxxxlotofcharactersinbase64"}),但没有任何效果。我错过了什么?
【问题讨论】:
-
InvokeApi是做什么的? -
@Cheesebaron 这是来自 MSDN 的文档:http://bit.ly/1UCn6LK 它从您的 Azure 移动服务调用 API;在我上面的例子中,_service 是 MobileServiceClient 的一个实例
-
您可以尝试使用 Fiddler 检查流量以查看发送的内容,以验证出错的位置。
标签: c# json azure asp.net-web-api xamarin.android