Reputation: 1288
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?
Upvotes: 1
Views: 19665
Reputation: 641
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:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "GetData",
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(DataRequest parameter);
public string GetData(DataRequest parameter)
{
// Do stuff
return "your data here";
}
DataRequest
)[DataContract(Namespace = "YourNamespaceHere")]
public class DataRequest
{
[DataMember]
public string ID{ get; set; }
[DataMember]
public string Data{ get; set; }
}
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;
}
Upvotes: 11
Reputation: 7067
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();
Upvotes: 2