有人能告訴我如何向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