tags:

views:

125

answers:

1

Hi,

I'm having a hard time to get a simple RESTful web service application working with my C# REST client. I'm trying to send pure XML using the POST method.

I have created a WCF Rest Service which is up and running (I can see the Service test page in my browser). Also I can call my WCF Rest Service via Fiddler and it responds correctly (I'll get a return value).

But the big problem is the REST client. I've done everything using all kinds of guides. In addition, the same client code works with the Java-based REST service. But in .NET all I get is the error "The remote server returned an error: (400) Bad Request." This exception happens after Request.GetResponse()

As I have developed WCF services I've used to specify configuration settings for both client (app.config) and server (web.config) but how is the situation with .NET-based RESTful web services? I mean, do I need to specify identical binding settings (webHttpBinding) for both, client (app.config) and server (web.config)?

Here is the client-side code:

XmlDocument TestDocument = new XmlDocument();
TestDocument.Load(XMLFilePath);
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(TestDocument.OuterXml);

Uri uri = new Uri("http://localhost:2319/MyRESTService.svc/receive");

HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(uri);     

Request.ContentLength = RequestBytes.Length;
Request.Method = "POST";
Request.ContentType = "text/xml";

Stream RequestStream = Request.GetRequestStream();
RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
RequestStream.Close();

HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ResponseMessage = reader.ReadToEnd();
response.Close();

The web.config for my WCF REST Service looks like this (should I specify the same settings in the app.config in the C# REST client?):

<system.serviceModel>
<services>
  <service name="MyRESTService.MyRESTService">
    <endpoint binding="webHttpBinding" contract="MyRESTService.IMyRESTService"
              behaviorConfiguration="webHttp"/>
   </service>
</services>

<behaviors>

  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

The interface for my WCF REST Service looks like this:

[ServiceContract]
public interface IMyRESTService

[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive",
  RequestFormat = WebMessageFormat.Xml,
  ResponseFormat = WebMessageFormat.Xml,
  BodyStyle=WebMessageBodyStyle.Bare)]
string Receive(string xml);

As I understand this is a client-side problem and the exception is raised due to problems found on the client-side. But what is specifically causing this exception in my C# Rest Client?

The XML looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<MATMAS02>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
......

Thanks!

A: 

This problem has been solved in the following post:

http://stackoverflow.com/questions/3372335/what-is-the-correct-uri-for-sending-parameters-via-post-in-wcf-rest-services