InvokeApiAsync 在 StringContent 上返回 415 (Monodroid / Xamarin.Android)
InvokeApiAsync returning 415 on StringContent (Monodroid / Xamarin.Android)
使用 Xamarin Studio,制作 Xamarin.Android 带有 Azure 移动服务 + .NET 后端自定义管理的应用程序 API (WebApi 2)。我知道我的移动服务正在运行,因为我可以在日志中看到我的应用程序正在访问它。这是我自定义的签名 api:
[HttpPost]
[Route("postplant")]
public HttpResponseMessage PostPlant([FromBody] string imageInBase64)
我正在尝试使用 InvokeApiAsync 触发此操作,尝试了一些重载但我正在尝试使用 Raw Http one。我的字符串是一个已转换为 base 64 的 jpg;如果我将字符串直接输入到我的移动服务测试站点,它工作正常。
我的问题是,我遇到了 415 不受支持的实体媒体类型 (text/plain) 错误。
Message='UserMessage='The request entity's media type 'text/plain' is
not supported for this resource.'', Status=415 (UnsupportedMediaType),
Exception=System.Web.Http.HttpResponseException: Processing of the
HTTP request resulted in an exception.
这是我在 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"}),但没有任何效果。我错过了什么?
解决了我的问题。图片已经在一个字符串中,但我必须像这样序列化它:
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 不受支持的实体媒体类型 (text/plain) 错误;不幸的是,客户端的调用仍然失败,调用冻结并且从未注册响应(尽管我的服务器确认已发回 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;
使用 Xamarin Studio,制作 Xamarin.Android 带有 Azure 移动服务 + .NET 后端自定义管理的应用程序 API (WebApi 2)。我知道我的移动服务正在运行,因为我可以在日志中看到我的应用程序正在访问它。这是我自定义的签名 api:
[HttpPost]
[Route("postplant")]
public HttpResponseMessage PostPlant([FromBody] string imageInBase64)
我正在尝试使用 InvokeApiAsync 触发此操作,尝试了一些重载但我正在尝试使用 Raw Http one。我的字符串是一个已转换为 base 64 的 jpg;如果我将字符串直接输入到我的移动服务测试站点,它工作正常。
我的问题是,我遇到了 415 不受支持的实体媒体类型 (text/plain) 错误。
Message='UserMessage='The request entity's media type 'text/plain' is not supported for this resource.'', Status=415 (UnsupportedMediaType), Exception=System.Web.Http.HttpResponseException: Processing of the HTTP request resulted in an exception.
这是我在 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"}),但没有任何效果。我错过了什么?
解决了我的问题。图片已经在一个字符串中,但我必须像这样序列化它:
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 不受支持的实体媒体类型 (text/plain) 错误;不幸的是,客户端的调用仍然失败,调用冻结并且从未注册响应(尽管我的服务器确认已发回 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;