0

Can some one tell me how to post request to a wcf rest service? my code is below

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;
}

but it gives following exception

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

and the value of message variable is

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

i know wcf rest is deprecated. But can some one pls tell me how to implement this? thx

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sat
  • 161
  • 1
  • 2
  • 8
  • You need to find out why the service is considering your request to be bad. Enable tracing at the server, and it should have some additional information. – carlosfigueira Dec 20 '12 at 04:06

2 Answers2

0

WCF by default uses SOAP as the message protocol, and from your sample it seems like you're doing HTTP with an XML body.

If you're using a normal WCF service, it would be easier to use a typical WCF client (created via a ChannelFactory or something similar) and use that to call the different operations.

If you're not using a typical WCF service, sharing your configuration / code could help get you na better answer.

Ramiro Berrelleza
  • 2,254
  • 1
  • 15
  • 27
0

Whenever you receive HTML and you expected data, the chances are that the HTML represents an error message from the server. Capture it and display it, and you'll see what the error is.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • thx for your reply.you are right an exception was thrown somewhere inside the server code but why should it say bad request all the time? so i thought something was wrong with the client! when i fixed the server code everything was normal. phew it took me almost a week :( – Sat Dec 23 '12 at 13:56