1

How can I send xml data via post with help of WCF ?

for example I have some code:

public interface IServiceForILobby
{
    [OperationContract]
    [WebInvoke(Method = "POST")] 
    string SendXml(string response);
}

//This is HOST

static void Main(string[] args)
{
    Console.WriteLine("*Console Based Rest HOST*");

    using (WebServiceHost serviceHost = new WebServiceHost(typeof(ServiceForILobby)))
    {
        serviceHost.Open();
        Console.ReadLine();                
    }

/*This is Client */

using (ChannelFactory<IServiceForILobby> cf = new   ChannelFactory<IServiceForILobby>(new WebHttpBinding(), "http://192.168.1.103:50000/RestService/SendXml?response={x}"))
{
    cf.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());
    IServiceForILobby channel = cf.CreateChannel();
    string s;
    // s = channel.SendXml("http://192.168.1.103:50000/RestService/SendXml?response={x}");
    string a;

    using (StreamReader sr = new StreamReader("simplexml.txt"))
    {
        string xmlCode = sr.ReadToEnd();
        s = channel.SendXml(xmlCode);
    }

I want to send XML from the Client to the Host, and after to parse it like this How does one parse XML files?

Community
  • 1
  • 1
Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31

2 Answers2

10

In order to send xml data through a POST, you need to construct your data correctly according to the WCF service. Here is basically what you need:

1) WCF Service Interface

[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "GetData",
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(DataRequest parameter);

2) WCF Service Implementation

public string GetData(DataRequest parameter)
{
    //Do stuff
    return "your data here";
}

3) Data Contract in your WCF service (In this case it's DataRequest)

[DataContract(Namespace = "YourNamespaceHere")]
public class DataRequest
{
    [DataMember]
    public string ID{ get; set; }
    [DataMember]
    public string Data{ get; set; }
}

4) Client sending the data must have the data constructed properly! (C# console app in this case)

static void Main(string[] args)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    string SampleXml = "<DataRequest xmlns=\"YourNamespaceHere\">" +
                                    "<ID>" +
                                    yourIDVariable +
                                    "</ID>" +
                                    "<Data>" +
                                    yourDataVariable +
                                    "</Data>" +
                                "</DataRequest>";

    string postData = SampleXml.ToString();
    byte[] data = encoding.GetBytes(postData);

    string url = "http://localhost:62810/MyService.svc/GetData";

    string strResult = string.Empty;

    // declare httpwebrequet wrt url defined above
    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
    // set method as post
    webrequest.Method = "POST";
    // set content type
    webrequest.ContentType = "application/xml";
    // set content length
    webrequest.ContentLength = data.Length;
    // get stream data out of webrequest object
    Stream newStream = webrequest.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    //Gets the response
    WebResponse response = webrequest.GetResponse();
    //Writes the Response
    Stream responseStream = response.GetResponseStream();

    StreamReader sr = new StreamReader(responseStream);
    string s = sr.ReadToEnd();

    return s;
}
Charlie Ou Yang
  • 631
  • 7
  • 16
2

The following snippet of code from c#-corner provides a good example:

    string postData = SampleXml.ToString();
    // convert xmlstring to byte using ascii encoding
    byte[] data = encoding.GetBytes(postData);
    // declare httpwebrequet wrt url defined above
    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
    // set method as post
    webrequest.Method = "POST";
    // set content type
    webrequest.ContentType = "application/x-www-form-urlencoded";
    // set content length
    webrequest.ContentLength = data.Length;
    // get stream data out of webrequest object
    Stream newStream = webrequest.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();
    // declare & read response from service
    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Seymour
  • 7,043
  • 12
  • 44
  • 51