【问题标题】:InvokeApiAsync returning 415 on StringContent (Monodroid / Xamarin.Android)InvokeApiAsync 在 StringContent 上返回 415 (Monodroid / Xamarin.Android)
【发布时间】: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


【解决方案1】:

解决了我的问题。图像已经在一个字符串中,但我必须像这样序列化它:

string jObj = JsonConvert.SerializeObject(imageInBase64);
StringContent content = new StringContent(jObj, System.Text.Encoding.UTF8);
MediaTypeHeaderValue mValue = new MediaTypeHeaderValue("application/json");
content.Headers.ContentType = mValue;

修复了我的 415 不支持的实体媒体类型(文本/纯文本)错误;不幸的是,客户端的呼叫仍然失败,在呼叫中冻结并且从未注册响应(尽管我的服务器确认已发回 200 响应)。

我最终通过从 InvokeApiAsync 中删除 async/await 来解决此问题;要么我没有正确使用它们,要么我的应用程序在尝试等待内容时失败。调用永远不会完成。相反,我更改了这些以获取异步 InvokeApiAsync 的结果。这是我的解决方案:

HttpResponseMessage resp = _service.InvokeApiAsync(@"jv/postplant", content, HttpMethod.Post, null, null).Result;
_service.Dispose();

string result = await resp.Content.ReadAsStringAsync();
JToken json = JToken.Parse(result);
return json;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多