如何使用 wcf 通过 post 发送 xml [英] How to send xml via post with wcf
问题描述
如何在 WCF 的帮助下通过 post 发送 xml 数据?
How can I send xml data via post with help of WCF ?
例如我有一些代码:
public interface IServiceForILobby
{
[OperationContract]
[WebInvoke(Method = "POST")]
string SendXml(string response);
}
//这是主机
static void Main(string[] args)
{
Console.WriteLine("*Console Based Rest HOST*");
using (WebServiceHost serviceHost = new WebServiceHost(typeof(ServiceForILobby)))
{
serviceHost.Open();
Console.ReadLine();
}
/*这是客户端*/
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);
}
我想将 XML 从客户端发送到主机,然后像这样解析它 如何解析 XML 文件?
I want to send XML from the Client to the Host, and after to parse it like this How does one parse XML files?
推荐答案
为了通过 POST 发送 xml 数据,您需要根据 WCF 服务正确构造您的数据.这基本上是您需要的:
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 服务接口
1) WCF Service Interface
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "GetData",
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(DataRequest parameter);
2) WCF 服务实现
2) WCF Service Implementation
public string GetData(DataRequest parameter)
{
//Do stuff
return "your data here";
}
3) WCF 服务中的数据契约(在本例中为 DataRequest)
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) 发送数据的客户端必须正确构造数据!(在本例中为 C# 控制台应用程序)
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;
}
这篇关于如何使用 wcf 通过 post 发送 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!