如何使用wcf通过post发送xml

问题描述 投票:0回答:2

如何在 WCF 的帮助下通过 post 发送 xml 数据?

例如我有一些代码:

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文件?

xml wcf post
2个回答
10
投票

为了通过 POST 发送 XML 数据,您需要根据 WCF 服务正确构造数据。这基本上就是您所需要的:

  1. WCF 服务接口
[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "GetData",
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(DataRequest parameter);
  1. WCF服务实施
public string GetData(DataRequest parameter)
{
    // Do stuff
    return "your data here";
}
  1. WCF 服务中的数据契约(在本例中为
    DataRequest
[DataContract(Namespace = "YourNamespaceHere")]
public class DataRequest
{
    [DataMember]
    public string ID{ get; set; }
    [DataMember]
    public string Data{ get; set; }
}
  1. 发送数据的客户端必须正确构造数据! (本例中为 C# 控制台应用程序)
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;
}

2
投票

以下来自 c#-corner 的代码片段提供了一个很好的示例:

    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();
© www.soinside.com 2019 - 2024. All rights reserved.