有人能告诉我如何向wcf rest服务发布请求吗?我的代码如下试图向wcf REST服务发布请求时发生意外的HTML响应
TResponse Post<TRequest, TResponse>(string uri, TRequset input){
HttpClient client = new HttpClient();
HttpContent content = new ObjectContent<TRequest>(input, new XmlMediaTypeFormatter());
HttpResponseMessage message = client.PostAsync(uri, content).Result;
return message.Content.ReadAsAsync<TResponse>().Result;
}
但它提供了以下异常
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/html'.
Source=System.Net.Http.Formatting
和消息变量的值是
message {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: Close
Cache-Control: private
Date: Wed, 19 Dec 2012 14:36:32 GMT
Server: ASP.NET
Server: Development
Server: Server/10.0.0.0
X-AspNet-Version: 4.0.30319
Content-Length: 1766
Content-Type: text/html
}} System.Net.Http.HttpResponseMessage
我知道WCF其余部分已被弃用。但是,有人可以告诉我如何实施? thx
你需要找出为什么服务正在考虑你的请求是坏的。在服务器上启用跟踪,它应该有一些额外的信息。 – carlosfigueira