本文介绍了没有MediaTypeFormatter可从内容与媒体类型读取类型的对象“字符串''text / plain的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这种情况:

他们是在 Servoy 一个外部web服务,我想在ASP.NET MVC applicatie使用此服务。

Their is a external webservice in Servoy and I want to use this service in a ASP.NET MVC applicatie.

通过这个code我尝试从该服务中的数据:

With this code I attempt to get the data from the service:

HttpResponseMessage resp = client.GetAsync("http://localhost:8080/servoy-service/iTechWebService/axws/shop/_authenticate/mp/112818142456/82cf1988197027955a679467c309274c4b").Result;
resp.EnsureSuccessStatusCode();

var foo = resp.Content.ReadAsAsync<string>().Result;

但是当我运行的应用程序,我得到了一个错误:

but when I run the application I get the next error:

没有MediaTypeFormatter可读取类型的对象字符串  从内容与媒体类型text / plain的。

如果我打开提琴手并运行相同的URL,我看到了正确的数据,但内容类型为text / plain的。但是我看到提琴手也是我想要的JSON ...

If I open Fiddler and run the same url, I see the right data but the content-type is text/plain. However I see in Fiddler also the JSON that I want...

是否有可能解决这个问题,在客户端或者是它的Servoy web服务?

Is it possible to solve this at client side or is it the Servoy webservice?

更新:
用于替代型Htt presponseMessage HttpWebRequest和阅读的StreamReader ...

Update:
Used HttpWebRequest instead of HttpResponseMessage and read the response with StreamReader...

推荐答案

尝试使用ReadAsStringAsync()来代替。

Try using ReadAsStringAsync() instead.

 var foo = resp.Content.ReadAsStringAsync().Result;

为什么ReadAsAsync()不工作,是因为ReadAsAsync&LT的原因;>将尝试使用默认的MediaTypeFormatter(即JsonMediaTypeFormatter,XmlMediaTypeFormatter,...)之一,阅读内容类型文本的内容/平原。然而,没有默认格式化可以读取文本/纯(它们只能读取应用/ JSON的,应用程序/ XML,等等)。

The reason why it ReadAsAsync() doesn't work is because ReadAsAsync<> will try to use one of the default MediaTypeFormatter (i.e. JsonMediaTypeFormatter, XmlMediaTypeFormatter, ...) to read the content with content-type of 'text/plain'. However, none of the default formatter can read the 'text/plain' (they can only read 'application/json', 'application/xml', etc).

通过使用ReadAsStringAsync(),内容将被解读为字符串的内容类型无关。

By using ReadAsStringAsync(), the content will be read as string regardless of the content-type.

这篇关于没有MediaTypeFormatter可从内容与媒体类型读取类型的对象“字符串''text / plain的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:56