Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So if you want to send raw data such as XML to your WCF REST Service (and also return), here is how to do it. But I have to say that before I found this solution I spend a lot of time googling frustrated as all examples were just talking about sending parameters in the URI (come on, the common scenario is to send XML and you can't do that properly in the URI). And when finally I found the right code examples it came up that it wasn't enough in WCF as I got the error "400 Bad Request". This error was caused by the fact that WCF can't use my raw XML if I don't force it by overriding it with some custom code (come on, what were you thinking Microsoft?, fix this in the next version of .NET Framework). So I'm not satisfied at all if doing such a basic thing can be so hard and time consuming). </p> <p>** IMyRESTService.cs (server-side code) ** </p> <pre><code>[OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)] Stream Receive(Stream text); </code></pre> <p>** client-side code **</p> <pre><code>XmlDocument MyXmlDocument = new XmlDocument(); MyXmlDocument.Load(FilePath); byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml); Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/Receive"); 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(); </code></pre> <p>** XmlContentTypeMapper.cs (server-side custom code which forces WCF to accept raw XML) **</p> <pre><code>public class XmlContentTypeMapper : WebContentTypeMapper { public override WebContentFormat GetMessageFormatForContentType(string contentType) { return WebContentFormat.Raw; } } </code></pre> <p>** Web.config (server-side configuration settings for utilizing the custom code)</p> <pre><code>&lt;endpoint binding="customBinding" bindingConfiguration="XmlMapper" contract="MyRESTService.IMyRESTService" behaviorConfiguration="webHttp" /&gt; &lt;bindings&gt; &lt;customBinding&gt; &lt;binding name="XmlMapper"&gt; &lt;webMessageEncoding webContentTypeMapperType="MyRESTService.XmlContentTypeMapper, MyRESTService"/&gt; &lt;httpTransport manualAddressing="true"/&gt; &lt;/binding&gt; &lt;/customBinding&gt; &lt;/bindings&gt; </code></pre> <p>Invoke a WCF Web Service with an HTTP POST <a href="http://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC" rel="nofollow noreferrer">http://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload